How can I randomly select a row from a matrix?
조회 수: 5 (최근 30일)
이전 댓글 표시
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
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
채택된 답변
추가 답변 (2개)
Wayne King
2013년 6월 17일
편집: Wayne King
2013년 6월 17일
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),:);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!