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 次查看(过去 30 天)
X = 0;
while (abs(X) < 3);
if(rand < 0.7)
X = X + 1;
disp('A');
else
X = X - 1;
disp('B');
end
end
X

采纳的回答

Jon
Jon 2019-8-30
编辑:Jon 2019-8-30
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 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by