How can I vectorize for loop with randsample

조회 수: 7 (최근 30일)
Merit
Merit 2012년 10월 13일
I like to know if there is a any simpler way of doing this (without for-loop):
A = zeros(4,3) %this size is just an example
for i=1 : 4 %I want to add 4 vectors ...
A(i,:) = randsample(7,3)' %... which are 3 different numb from [1-7]
end
A =
4 6 2
1 7 3
2 1 5
3 5 6
So my point is to create a matrix from different vectors, which I have created with randsample. Is there a way I can vectorize this?
Have been fighting with this many days :)
Yours Merit

채택된 답변

Walter Roberson
Walter Roberson 2012년 10월 13일
편집: Walter Roberson 2012년 10월 13일
numvecs = 4;
popsize = 7;
numselected =3;
[junk, randidx] = sort(rand(numvecs, popsize), 2); %sort along row
A = randidx(:,1:numselected);
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2012년 10월 13일
+1
Merit
Merit 2012년 10월 13일
Thank you Walter, just what I needed :)
Merit

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel Computing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by