Unrepeated random numbers

조회 수: 2 (최근 30일)
Raviteja
Raviteja 2012년 2월 13일
편집: animati 2013년 10월 10일
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
Raviteja
Raviteja 2012년 2월 13일
I got it...
>>S(randperm(5))
Sean de Wolski
Sean de Wolski 2012년 2월 13일
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.

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

답변 (1개)

Jonathan Sullivan
Jonathan Sullivan 2012년 2월 13일
L = length(S);
k = 5;
ind = randperm(L,k);
S_rand = S(ind);

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by