How do I get my code to run multiple times? My code looks as follows. My code shows X = 3 when A wins and X = -3 when B wins. I would like to get this code to run multiple times and see how many times A wins out of 10 or so matches.
1 view (last 30 days)
Show older comments
X = 0;
while (abs(X) < 3);
if(rand < 0.7)
X = X + 1;
disp('A');
else
X = X - 1;
disp('B');
end
end
X
0 Comments
Accepted Answer
Jon
on 30 Aug 2019
Edited: Jon
on 30 Aug 2019
You don't need a loop for this, just do something like the following
N = 10 % number of trials
X = rand(N,1)
Awins = sum(X<0.7) % number of times A wins
Bwins = N - Awins % number of times B wins
Note X<0.7 is a vector of ones and zeros (true and false) with a 1 on each trial where a wins and a zero on each trial where A does not win (B wins). Summing X<0.7 just adds up the total times A wins.
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!