How do I index a variable based on the outcome of a logical expression (e.g if x +y = 2 then variable 2 is indexed)
10 views (last 30 days)
Show older comments
I have an if statement in a for loop which cycles through a row vector, counting the total number of each sum between n elements of both vectors. It is based off of rolling dice so the max value is 6 and the range of the sums can be from 2-12.
For example if the 5th element of array1 was 4 and the 5th element of array2 was 5 then the sum would be 9 and the total number of 'nines' in the sums of the row vectors would increase by 1.
I know how to do this using 11 elseif statements, however how can I do it just using one if/else statement.
In my head it works like this:
- nth element of array 1 and 2 are added together and give x
- use value of x to index corresponding tx variable. so if x = 4, variable t4 would be substituted into the if statement
- continue by adding 1 to the tx value until 300 loops have been completed.
Would there be a way to do it like this?
if (array1(n) + array2(n)) == x %Where x = an integer 2-12
tx = tx+1
Or is this simply not possible and must I use 11 seperate elseif statements for this? I am aware this is a simple problem to have but I am just curious as to whether you can write code to substitute different variables into different places as the code is running. I am fairly new to matlab with just a month or two's experience so do bear with me! Thanks
here is my code currently:
array1 = randi(6,1,300);
array2 = randi(6,1,300);
t2 = 0;
t3 = 0;
t4 = 0;
t5 = 0;
t6 = 0;
t7 = 0;
t8 = 0;
t9 = 0;
t10 = 0;
t11 = 0;
t12 = 0;
for n=1:300
if (array1(n) + array2(n)) == 2
t2 = t2+1;
elseif (array1(n) + array2(n)) == 3
t3 = t3+1;
elseif (array1(n) + array2(n)) == 4
t4 = t4+1;
... % ... shows that elseif statements continue until t12=t12+1
end
end
1 Comment
Stephen23
on 6 Dec 2023
Edited: Stephen23
on 6 Dec 2023
"Would there be a way to do it like this?"
Yes, if you want to force yourself into writing slow, complex, inefficient, insecure, obfuscated, buggy code that is hard to debug:
The MATLAB approach is very simple: use a vector and indexing. For example, replace all of these seperate scalar values (which are bad data design because you a forcing pseudo-indices into variable names):
t2 = 0;
t3 = 0;
t4 = 0;
t5 = 0;
t6 = 0;
t7 = 0;
t8 = 0;
t9 = 0;
t10 = 0;
t11 = 0;
t12 = 0;
with just one vector:
T = zeros(1,12);
and then use simple, basic, efficient, robust, real indexing.
Answers (1)
Steven Lord
on 6 Dec 2023
Here's an example with one die. You can imagine how much more code than this you'd have to use if you wanted to count the frequencies of rolls of a 100-sided die (as an example) using your approach! In my code handling a d100 or d% would require changing one line. You should be able to adapt this to your two die example.
numSides = 6;
rollCounts = zeros(1, numSides);
numRolls = 1e4;
for k = 1:numRolls
roll = randi(numSides);
rollCounts(roll) = rollCounts(roll)+1;
end
disp(rollCounts)
% check that each roll was counted
isequal(sum(rollCounts), numRolls)
Of course there are other ways to do this that don't involve calling randi numRolls times. Using histcounts is one potential solution.
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!