create multiple separate vectors from a For loop

45 views (last 30 days)
I want to create a for loop that makes a pre-specified amount of passes through the loop. I have done that, but the results end up in an array. I want each pass through the loop to make a new vector. For example, I want to roll three dice, 200 times each, I then want three vectors created labelled dice1, dice2, dice3, each with 200 values.
for n = 1 : diceAmount
if (diceType == 1)
diceSelected=("d4 Selected")
diceRoll{n} = randi(4,1,200)
i already have diceAmount, and diceType working properly, the rest of the for loop allows for different dice selections.
  3 Comments
Kyle Rapinchuk
Kyle Rapinchuk on 20 Nov 2020
The output currently is a 1 by n cell with n being the amount of dice chosen earlier in the script. Each cell has a 1 by 200 vector in it. I want all rolls to be treated as seperate dice so that if three dice are chosen I will end up with three 1x200 vectors. Each one labelled Dice1, Dice2....DiceN. I need to act as if the dice are being rolled simultaneously each turn. For example if three dice are chosen then in spot 83 of the three arrays that will be as if the three dice were rolled at the same time for the 83rd time. Those results will later be compared against one another.
list = {'d4','d6','d8','d10','d12','d20'}
[diceType,tf] = listdlg('ListString',list,'SelectionMode','single','PromptString','Choose The Dice')
prompt = {'How Many Dice?'}
dlgtitle = ''
dims = [1 20]
dieAmount = inputdlg(prompt,dlgtitle,dims)
diceAmount=str2num(dieAmount{1})
for n = 1 : diceAmount
if (diceType == 1)
diceSelected=("d4 Selected")
diceRoll{n} = randi(4,1,200)
elseif (diceType == 2)
diceSelected=("d6 Selected")
diceRoll{n} = randi(6,1,200)
I hope I am making sense
Steven Lord
Steven Lord on 20 Nov 2020
Storing the dice in three separate cells in diceRoll will make it more difficult to work with all diceAmount dice "in spot 83 of the three arrays". The code to compute score in the example I wrote for my answer would be much more complicated with your approach.

Sign in to comment.

Answers (2)

Ameer Hamza
Ameer Hamza on 20 Nov 2020
Edited: Ameer Hamza on 20 Nov 2020
You current approach is optimal. Creating seperate variables like dice1, dice2, .. is not a good coding approach: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. It will make your code inefficient and hard to maintain or debug.
  2 Comments
Kyle Rapinchuk
Kyle Rapinchuk on 20 Nov 2020
If that's the case is there then a way after the cell is created to extract the data into new variables because that data needs to be used after to see if each roll is over a certain number, a number that is inputed by the user. I need to act as if the dice are being rolled simultaneously each turn. For example if three dice are chosen then in spot 83 of the three arrays that will be as if the three dice were rolled at the same time for the 83rd time. Those results will later be compared against one another, and against a given value. Those values then need to be compared as a percentage of if they are over the inputed value or "hits". A "hit" will then become a 1, and a "miss" will become a 0. Then all of the hits will be rolled again and the new numbers will be placed into a new vector.
Stephen23
Stephen23 on 20 Nov 2020
"...is there then a way after the cell is created to extract the data into new variables because that data needs to be used after to see if each roll is over a certain number, a number that is inputed by the user."
Either way you have exactly the same data available to you, so either way you can do that comparison. The approach you are trying will be more complex and much less effiicient than simply accessing the data directly from the cell array.

Sign in to comment.


Steven Lord
Steven Lord on 20 Nov 2020
Rather than creating several different variables with numbered names or even a cell array, I'd prefer to create an array.
nsides = 6;
ndice = 4;
ntrials = 10;
A = randi(nsides, [ntrials ndice])
A = 10×4
4 6 3 6 4 2 3 4 5 5 3 2 4 3 6 3 5 3 5 6 1 2 3 2 3 3 6 5 5 3 6 3 1 3 6 3 2 3 4 2
Doing this lets you operate on a set of rolls all at once. For instance, if I wanted to take the highest three of each set of four dice and add them up:
score = sum(A, 2)-min(A, [], 2)
score = 10×1
16 11 13 13 16 7 14 14 12 9
If I wanted to check if the "first die" was fair or not, I could do that.
B = randi(nsides, [1e6 ndice]);
histogram(B(:, 1))
Looks pretty uniform to me.
C = randi(nsides+1, [1e6 1]);
C(C == nsides+1) = 1;
histogram(C)
Looks very biased (as expected from the way I constructed C.)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!