How can I randomly select a row from a matrix?

20 次查看(过去 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
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
Try this:
ind = ceil(rand * size(m,1));
mrow = m(ind,:);
  5 个评论
Evan
Evan 2013-6-17
No worries! And you're welcome--glad you got it fixed. :)
Sushmita kumari
Sushmita kumari 2022-2-11
i i wish to find a coloum insted of row .please suggest sutable code

请先登录,再进行评论。

更多回答(2 个)

Jonathan Sullivan
Jonathan Sullivan 2013-6-17
Try using randi
Example
randomRow = m(randi(size(m,1)),:);

Wayne King
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),:);

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by