How can I vectorize for loop with randsample

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

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 13 Okt. 2012
Bearbeitet: Walter Roberson am 13 Okt. 2012

2 Stimmen

numvecs = 4;
popsize = 7;
numselected =3;
[junk, randidx] = sort(rand(numvecs, popsize), 2); %sort along row
A = randidx(:,1:numselected);

2 Kommentare

Andrei Bobrov
Andrei Bobrov am 13 Okt. 2012
+1
Merit
Merit am 13 Okt. 2012
Thank you Walter, just what I needed :)
Merit

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by