randperm subset of random m-by-n matrix

2019-01-29 11:23发布

how do you use randperm to select a m-by-n matrix of data and output a smaller subset of that random data?

i.e

newfile = randperm(data(m = 2000, n = 3));  %% save random m-by-n data as newfile

1条回答
We Are One
2楼-- · 2019-01-29 11:49

Consider this example:

data = rand(8,6);
m = 4;
n = 2;

%# pick random rows
indX = randperm( size(data,1) );
indX = indX(1:m);

%# pick random columns
indY = randperm( size(data,2) );
indY = indY(1:n);

%# filter data
data2 = data(indX,indY)

Note that R2011b introduced an easier syntax for RANDPERM, allowing you to directly write:

indX = randperm(size(data,1), m);
indY = randperm(size(data,2), n);
查看更多
登录 后发表回答