How can I randomly select a row from a matrix?

6 visualizaciones (últimos 30 días)
Graeme
Graeme el 17 de Jun. de 2013
Comentada: Sushmita kumari el 11 de Feb. de 2022
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 comentario
chaitra kn
chaitra kn el 17 de Ag. de 2019
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

Iniciar sesión para comentar.

Respuesta aceptada

Evan
Evan el 17 de Jun. de 2013
Editada: Evan el 17 de Jun. de 2013
Try this:
ind = ceil(rand * size(m,1));
mrow = m(ind,:);
  5 comentarios
Evan
Evan el 17 de Jun. de 2013
No worries! And you're welcome--glad you got it fixed. :)
Sushmita kumari
Sushmita kumari el 11 de Feb. de 2022
i i wish to find a coloum insted of row .please suggest sutable code

Iniciar sesión para comentar.

Más respuestas (2)

Jonathan Sullivan
Jonathan Sullivan el 17 de Jun. de 2013
Try using randi
Example
randomRow = m(randi(size(m,1)),:);

Wayne King
Wayne King el 17 de Jun. de 2013
Editada: Wayne King el 17 de Jun. de 2013
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),:);

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by