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

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

 채택된 답변

[idx,idx]=sort(rand(1,n));
out=idx(1:k)

댓글 수: 1

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by