How to select particular rows of a matrix?

I have a matrix (say 100 x 33). I need to select particular rows of the matrix wherein the row numbers are given by a random function.

Answers (2)

This is basic matrix indexing that is covered in the getting started tutorial.
m = reshape(1:100*33, 33, 100)'; %demo matrix
rowindices = randperm(size(m, 1), 5); %demo data, choose 5 rows at random
mrows = m(rowindices, :)
This works:
M = randi(99, 100, 33); % Create Matrix
Rows = randi(size(M,1), 10, 1); % Randomly Choose 10 Rows
Out = M(Rows,:);

3 Comments

My pleasure!
If my Answer solved your problem, please Accept it.
I would use randperm instead of randi so that you are guaranteed to select different rows

Sign in to comment.

Asked:

on 28 Jan 2016

Commented:

on 28 Jan 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!