Random numbers and storing from 1 -5
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
BionicP
am 7 Feb. 2021
Kommentiert: BionicP
am 7 Feb. 2021
Hi, Trying to create a random number gen from 1-5 and store into an array. I want the random numbers to generate 600 times giving me an 100 random 1's,2's,3's,4's and 5's. I keep getting size issues of left and right, anyone able to help? I should end up with 600 random distributions of 1-5 but it stops on the first loop with "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
Thanks in advance
x=[];
s = 600;
y = [];
for i=1:1:s
y(i)=randperm(5);
y(i) = transpose(y(i));
x = vertcat(x,y(i));
end
0 Kommentare
Akzeptierte Antwort
the cyclist
am 7 Feb. 2021
I believe this does what you want:
s = 600;
x = zeros(5,s);
for i=1:1:s
x(:,i)=randperm(5)';
end
9 Kommentare
the cyclist
am 7 Feb. 2021
OK. I think I would do the second randomization method as follows:
y = repmat(1:5,1,100);
y = y(randperm(500))
If you prefer to do the third randomization method, a straightforward approach would be
y = randi(5,1,500)
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!