Random numbers and storing from 1 -5
6 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
the cyclist
on 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 Comments
the cyclist
on 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)
More Answers (1)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!