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 vue (au cours des 30 derniers jours)
X = 0;
while (abs(X) < 3);
if(rand < 0.7)
X = X + 1;
disp('A');
else
X = X - 1;
disp('B');
end
end
X

Réponse acceptée

Jon
Jon le 30 Août 2019
Modifié(e) : Jon le 30 Août 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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by