Coin Flip plotting every flip
Afficher commentaires plus anciens
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)
Réponses (1)
Walter Roberson
le 21 Nov 2015
Before the end of the while, add
plot(n-1, R, '*'); hold on
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!