Coin Flip plotting every flip
Mostrar comentarios más antiguos
I want to be able to plot the number of heads versus the number of flips using a while loop. I'm at the point where the while loop will tell me how many heads and tails I've gotten in the end but is there any way to plot it so it will show on the plot that, for example, there was heads on the first and third flip so the coordinates of the points on the plot that weren't horizontal lines were at (1,1) and (3,2)?
Here's my function code:
function [H,T]=coinflip(k)
n=1;
H=0;
T=0;
while n <= k
n=n+1;
R = randi([1,2]);
if R == 1
H=H+1;
disp('H')
end
if R == 2
T=T+1;
disp('T')
end
end
end
and my main code:
clear all, close all, clc
prompt='How many times do you want to flip the coin?';
k=input(prompt)
[H,T]=coinflip(k)
Respuestas (1)
Walter Roberson
el 21 de Nov. de 2015
Before the end of the while, add
plot(n-1, R, '*'); hold on
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!