How can I randomly select a row from a matrix?

I have a matrix (m) that is 17543 x 17. The values are of the type double.
I want to randomly select 1 row from this matrix and save it as a new vector.
I tried this:
mrow = m(randsample(m:17543,1),:)
This works if the values are integers but it does not work because they are doubles.
It returns the error message:
"Subscript indices must either be real positive integers or logicals."
Any help would be really appreciated.
Thanks,
Graeme

댓글 수: 1

chaitra kn
chaitra kn 2019년 8월 17일
this is for to select only first row,how can i select more than one random rows in two 2 matrix.
please help me out

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

 채택된 답변

Evan
Evan 2013년 6월 17일
편집: Evan 2013년 6월 17일

3 개 추천

Try this:
ind = ceil(rand * size(m,1));
mrow = m(ind,:);

댓글 수: 5

Graeme
Graeme 2013년 6월 17일
편집: Graeme 2013년 6월 17일
This seems to choose the first row everytime.
I changed it to:
ind = ceil(rand * size(m:17543));
mrow = m(ind,:);
It now selects the first row everytime and another random row.
Do you know how to get around this?
Evan
Evan 2013년 6월 17일
편집: Evan 2013년 6월 17일
Are you sure? I just tried this out with a random sample of size 17543x17 and I'm getting different rows each time. When you run my code, does the value of "ind" always return 1? I think it would only return 1 every time if m was a 1xn matrix.
Graeme
Graeme 2013년 6월 17일
Apologies you are correct.
Thanks you very much for your help!
Evan
Evan 2013년 6월 17일
No worries! And you're welcome--glad you got it fixed. :)
i i wish to find a coloum insted of row .please suggest sutable code

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

추가 답변 (2개)

Jonathan Sullivan
Jonathan Sullivan 2013년 6월 17일

2 개 추천

Try using randi
Example
randomRow = m(randi(size(m,1)),:);
Wayne King
Wayne King 2013년 6월 17일
편집: Wayne King 2013년 6월 17일

0 개 추천

m = randn(17543,17);
idx = randperm(size(m,1),1);
B = m(idx,:);
idx tells you which row you randomly selected.
If you have an older version of MATLAB where the above does not work do:
m = randn(17543,17);
idx = randperm(size(m,1),1);
B = m(idx(1),:);

카테고리

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

제품

질문:

2013년 6월 17일

댓글:

2022년 2월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by