How to generate k unique random numbers from 1 to n in matlab 2009

1 vue (au cours des 30 derniers jours)
chanukya
chanukya le 27 Avr 2013
Hi, I knew randperm(n,k) works but this is after 2011. Can some one tell me if there is any efficient way to do it in matlab 2009.
Thanks

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 27 Avr 2013
[idx,idx]=sort(rand(1,n));
out=idx(1:k)
  1 commentaire
Jan
Jan le 27 Avr 2013
The result is biased, because Matlab's sort is stable. The effects are very tiny and they appear in Matlab's randperm function with 1 input argument also.

Connectez-vous pour commenter.

Plus de réponses (2)

Zhang lu
Zhang lu le 27 Avr 2013
a=randperm(n); b=a(1:k)

Jan
Jan le 27 Avr 2013
Not biased and faster than sorting a RAND vector: http://www.mathworks.com/matlabcentral/fileexchange/27076-shuffle

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by