have to plot the number of head in a game of head or tails that the token is trowed N times, but my plot dont work

1 view (last 30 days)
the plot has to show the number of faces per time the token is throwed
N = input('Coloque um valor para N: ');
cara = 0;
coroa = 0;
x=rand(1,N);
for i = 1:length(x)
if x(i) < 0.5
x(i) = 0; % cara
else
x(i) = 1; % coroa
end
end
figure
histogram(x);
title('Gráfico')
for i = 1: length(x)
if x(i) == 0
caras = cara + 1;
else
coroa = coroa + 1;
end
end
plot(cara , length(x))

Accepted Answer

Voss
Voss on 29 Jul 2022
Edited: Voss on 29 Jul 2022
% caras = cara + 1;
cara = cara + 1;
That will get the total number of heads (a.k.a. faces) to be correct, but you'll still be plotting only one point (which won't show up without a data marker).
To plot the number of heads vs the number of token throws (a.k.a. coin tosses a.k.a. coin flips) for the entire sequence of flips:
plot(cumsum(x==0))

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!