i have to plot the percentage of heads by the times token is trowed times, in a game of head or tail, but a need help for doing that. What can I use in my plot?

1 visualización (últimos 30 días)
------- the token is throwed n times, and I also need to know how to add a line at 50 percent at my plot
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
cara = cara + 1;
else
coroa = coroa + 1;
end
end
plot(cumsum(x==0))

Respuesta aceptada

Torsten
Torsten el 29 de Jul. de 2022
Editada: Torsten el 29 de Jul. de 2022
N = 500;
x=rand(1,N);
for i = 1:length(x)
if x(i) < 0.5
x(i) = 1; % cara
else
x(i) = 0; % coroa
end
end
plot((1:N),100*cumsum(x)./(1:N))
  1 comentario
Image Analyst
Image Analyst el 30 de Jul. de 2022
@LUIS Clarification note: at the end, this will give you the probability for one experiment of tossing it 500 times. It does not give you the probability as a function of N (my code doesn't either) where N varies. If that's what you want, that's fine. And if the N is small, like 20, you can see from this one experiment's plot, it might give a probability of 30% instead of 50%. That's why I said to toss it 20 times like a million times - to get the true probability for 20 which should be 50%. But if what you want is to see the fraction as it approaches 50% as the number of tosses increases, then this shows it to you. Just be aware that the count for a certain number of tosses is not the probability for that number of tosses.
If you want a probability for all N, you'd need another, outer loop to have N vary.
for N = 2 : 500
instead of having N be fixed at some number like 20 or 500. So in essence you'd be building up an array giving the final value only of the plot above as N increases.

Iniciar sesión para comentar.

Más respuestas (2)

Image Analyst
Image Analyst el 29 de Jul. de 2022
You need a nested for loop. The inner loop will throw the token/coin N (or n) times and give you a percentage for that one experiment. Now you need to do that experiment a lot of times, like a million times or so, and that will be the outer loop
numExperiments = 1000000;
numTossesPerExperiment = N;
for k1 = 1 : numExperiments
countsThisExperiment = 0; % Reset counter.
% Toss coin N times counting the number of heads.
for k2 = 1 : N
% to do
end
% Record the percentage for this experiment
counts(k1) = nnz(x) / N;
end
histogram(counts);
grid on;

Voss
Voss el 30 de Jul. de 2022
% N = input('Coloque um valor para N: ');
N = 2500;
x = round(rand(1,N));
figure
histogram(x);
title('Total #')
xticks([0 1])
xticklabels({'cara','coroa'})
figure()
plot(cumsum(x==0))
title('# of cara vs # of throws')
xlabel('throws')
ylabel('cara')
figure
plot(cumsum(x==0)./(1:N))
hold on
plot([1 N],[0.5 0.5],'--r')
title('proportion of cara')
xlabel('throw #')

Categorías

Más información sobre Line Plots 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!

Translated by