How do I show the for loop results over time?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Chase Williams
el 10 de Mzo. de 2019
Editada: Kevin Phung
el 10 de Mzo. de 2019
I need to create a grapgh of player 2's score over the number of "trials". However, I can only see the last score of the 10,000 trial. How can I save all of the data such that I know the score for the nth trial?![Mathworks Q.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207679/Mathworks%20Q.png)
![Mathworks Q.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/207679/Mathworks%20Q.png)
0 comentarios
Respuesta aceptada
Kevin Phung
el 10 de Mzo. de 2019
Editada: Kevin Phung
el 10 de Mzo. de 2019
n = 100000; %number of trials
score = zeros(n,2); % first column is player 1, 2nd column is player 2
for i = 1:n
p1 = randi([1 2])
p2 = randi([1 2])
if p1+ p2 == 3
score(i,:) = [ 1 0];
else
score(i,:) = [0 1];
end
end
%check score for 99th trial:
score(99,:)
%check total score:
sum(score)
2 comentarios
Kevin Phung
el 10 de Mzo. de 2019
Editada: Kevin Phung
el 10 de Mzo. de 2019
plot(score(:,2)).
I would suggest you read up on array indexing
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!