How can I store each roll from a for loop as a matrix?

1 view (last 30 days)
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

Accepted Answer

OCDER
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
OCDER
OCDER on 29 Sep 2017
Edited: OCDER on 29 Sep 2017
Okay, fixed the answer to do it in a for loop. Using D_s cell array will allow you to save rounds with varying number of RollsPerRound.

Sign in to comment.

More Answers (1)

AmberJohnson
AmberJohnson on 29 Sep 2017
I have to generate random numbers for each round

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!