Coin Flip plotting every flip

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)

Risposte (1)

Before the end of the while, add
plot(n-1, R, '*'); hold on

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Richiesto:

il 21 Nov 2015

Risposto:

il 21 Nov 2015

Community Treasure Hunt

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

Start Hunting!

Translated by