Unrepeated random numbers

2 visualizaciones (últimos 30 días)
Raviteja
Raviteja el 13 de Feb. de 2012
Editada: animati el 10 de Oct. de 2013
I have a vector S
>> S=[1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 1.10];
I want to randomly pic 5 elements which are unrepeated.
How to do that?
  2 comentarios
Raviteja
Raviteja el 13 de Feb. de 2012
I got it...
>>S(randperm(5))
Sean de Wolski
Sean de Wolski el 13 de Feb. de 2012
No, I don't think that's what you want. That will always pick the first 5 elements in a new order, the 6th-nth elements will never be selected.
Jonathan Sullivan's solution appears to meet your question's intent.

Iniciar sesión para comentar.

Respuestas (1)

Jonathan Sullivan
Jonathan Sullivan el 13 de Feb. de 2012
L = length(S);
k = 5;
ind = randperm(L,k);
S_rand = S(ind);

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