Choose randomly rows in a matrix

Hi I have a matrix with 2 columns, the first is for 'ID', the second is for 'age'. I want to randomly pick 'n'(e.g. 2) IDs of the matrix and return the IDs and the age of the same row.
Thanks

답변 (1개)

Sean de Wolski
Sean de Wolski 2013년 6월 12일

0 개 추천

doc randperm
Then use the output of this as an index into your matrix.
x = magic(10);
idx = randperm(10,2)
x(idx,:)

댓글 수: 3

Vanessa
Vanessa 2013년 6월 13일
when I write idx=randperm(10,2)
??? Error using ==> randperm Too many input arguments.
You are using an old version of MATLAB that does not support that syntax for randperm. Instead use
idx = randperm(size(x,1));
x(idx(1:2),:)
Vanessa
Vanessa 2013년 6월 13일
it works :) thanks Walter

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2013년 6월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by