How can I store each roll from a for loop as a matrix?
1 view (last 30 days)
Show older comments
I would like to store each roll from my for loop as a row in a matrix so I can refer to this matrix later. Also c may decrease later.
if true
% code
end
Number of rounds= input ('Rounds?:')
c=10
for f=1:1:Number of rounds
D_s=randi(10,1,c);
end
0 Comments
Accepted Answer
OCDER
on 29 Sep 2017
Edited: OCDER
on 29 Sep 2017
Do you need to generate random numbers by round, or can you generate them all at once? If you can do the latter, here's one solution:
NumRounds = round(input('Rounds? '));
if NumRounds < 1 %In case user puts a wrong input
error('NumRounds cannot be negative');
end
RollsPerRound = 10; %This was your c, but c had no meaning - hence RollsPerRound is used.
D_s = cell(NumRounds, 1); %Storing results in cell array, in case RollsPerRound changes
for j = 1:length(D_s)
D_s{j} = randi(10, 1, RollsPerRound);
end
1 Comment
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!