Asked by Selin Soguksu
on 12 Dec 2012

Hello, I have an 10000 rows and 10 columns matrix. I want to select randomly 500 rows from this matrix. I want to ask you, randperm function is true for this purpose. How can I select 500 rows randomly from this matrix?

*No products are associated with this question.*

Answer by Jos (10584)
on 12 Dec 2012

Accepted answer

Random can be defined in two ways:

% A is your original matrix Nrows = size(A,1) ; % number of rows

% Option 1: randomly select 500 UNIQUE(!) rows idx = randperm(Nrows) ; idx = idx(1:500) ;

% Option 2: randomly select 500 rows idx = randi(Nrows,[Nrows 1]) ;

% and then ... B = A(idx,:) ;

Selin Soguksu
on 12 Dec 2012

Answer by Jan Simon
on 12 Dec 2012

Edited by Jan Simon
on 12 Dec 2012

M = rand(10000, 10); index = randperm(10000, 500); % In modern Matlab versions R = M(index, :);

In older Matlab versions `randperm` does not accept a 2nd input. Then:

index = randperm(10000); index = index(1:500);

If this must be fast, use the C-Mex FEX: Shuffle.

index = Shuffle(10000, 'index', 500)

Show 4 older comments

Image Analyst
on 14 Dec 2012

I could hear the sound of a hand slapping a forehead all the way across the Atlantic. ;-)

Opportunities for recent engineering grads.

## 0 Comments