Q:

How to select random samples from a dataset in Matlab

Possible Duplicate:
How do I randomly select k points from N points in MATLAB? 

Let's say I have a dataset that includes 10,000 rows of data. What is the best way to create a subset that includes 1,000 randomly chosen rows?

A:

You can use randperm for this task:
Sampling without replacement: 
nRows = 10000; % number of rows
nSample = 1000; % number of samples

rndIDX = randperm(nRows); 

newSample = data(rndIDX(1:nSample), :); 

Sampling with replacement: 
nRows = 10000; % number of rows
nSample = 1000; % number of samples

rndIDX = randi(nRows, nSample, 1); 

newSample = data(rndIDX, :); 

A:

Use randperm in combination with the number of rows. If x is your matrix:
nrows = size(x,1);
nrand = 1000; % Choose 1000 rows
assert(nrand<=nrows, 'You cannot choose more rows than exist in the matrix');
rand_rows = randperm(nrows, nrand);
xx = x(rand_rows,:);  % Select the random rows from x

A:

If you have the statistics toolbox R2012+, you can use datasample.
subset = datasample(data,1000)

subset will be a randomly selected subset of data consisting of 1000 samples.
To sample without replacement, use:
subset = datasample(data,1000,'Replace',false)

If you have an older version of the toolbox, you can use randsample:
rndIdx = randsample(size(data,1),1000,true); % with replacement
subset = samples(rndIdx(1:1000), :);

rndIdx = randsample(size(data,1),1000,false); % without replacement
subset = samples(rndIdx(1:1000), :);

But using randsample is more or less the same as H.Muster's answer (which I have accepted as the best because it doesn't require any toolbox).
Note: For more info on the
    difference between sampling with replacement vs. sampling without
    replacement, see this page.

