randperm subset of random m-by-n matrix

2019-01-29 10:51发布

问题:

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:

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);