Borrar filtros
Borrar filtros

Choose randomly rows in a matrix

1 visualización (últimos 30 días)
Vanessa
Vanessa el 12 de Jun. de 2013
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

Respuestas (1)

Sean de Wolski
Sean de Wolski el 12 de Jun. de 2013
doc randperm
Then use the output of this as an index into your matrix.
x = magic(10);
idx = randperm(10,2)
x(idx,:)
  3 comentarios
Walter Roberson
Walter Roberson el 13 de Jun. de 2013
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 el 13 de Jun. de 2013
it works :) thanks Walter

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by