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 visualización (últimos 30 días)
X = 0;
while (abs(X) < 3);
if(rand < 0.7)
X = X + 1;
disp('A');
else
X = X - 1;
disp('B');
end
end
X

Respuesta aceptada

Jon
Jon el 30 de Ag. de 2019
Editada: Jon el 30 de Ag. de 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.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by