How can I calculate 5 lions in a row?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
S.M
el 13 de Oct. de 2022
Respondida: Image Analyst
el 14 de Oct. de 2022
Question:A fair coin is tossed 10 times. What is the probability of getting exactly 5 lions in a row? ( 6 times in a row Don't count the number of taps or more.)
my code:
clear all
close all
clc
for i=0:1023
x=fliplr(de2bi(i));
coin(i+1,:)=[ zeros(1,10-length(x)) x ];
end
% =============================================
z=0;
yek=zeros(1,1024);
for i=1:1024
x=coin(i,:);
for j=1:10
if(x(:,j)==1)
z=z+1;
yek(1,i)=z;
end
end
z=0;
end
yek=yek';
y=zeros (253,1);
m=0;
for i=1:1024
if yek(i,:)>=5
y(m+1,1)=i;
m=m+1;
end
end
c=0;
q=0;
for i=y'
x=coin(i,:);
c=0;
n=0;
% ===============================
for j=1:9
if x(1,j)==1 && x(1,1+j)==1
c=c+1;
else c=0;
end
if c==4
n=4;
end
end
if n==4
q=q+1;
end
w(i,1)=c;
e(i,:)=x ;
end
my question:How can I extract 5 lions in a row through generated states?
0 comentarios
Respuesta aceptada
Torsten
el 13 de Oct. de 2022
Editada: Torsten
el 13 de Oct. de 2022
n = 1024;
S = 0:(n-1); % 2^10 possible sequences of coin tosses.
A = de2bi(S);
A = [zeros(n,1),A,zeros(n,1)];
nn = 0;
for i = 1:size(A,1)
idx = strfind(A(i,:),[0 1 1 1 1 1 0]);
if ~isempty(idx)
nn = nn + 1;
end
end
format long
fraction = nn/n
2 comentarios
John D'Errico
el 13 de Oct. de 2022
But there is no need to do a large simulation though, since the entire problem space contains only 1024 possible sequences, each of which is equally probable.
Más respuestas (2)
John D'Errico
el 13 de Oct. de 2022
Editada: John D'Errico
el 13 de Oct. de 2022
I'm so used to coins having heads or tails though. ;-) I guess I can deal with lions.
Anyway, your code gets at the question at hand, but you can make it simpler.
S = 0:1023; % 2^10 possible sequences of coin tosses.
sequences = de2bi(S)
I dropped the semicolon so we could see the first few sequences. All you need now is to count the number of sequences where there were at least 5 tosses in a row.
Probably it is easiest here to just use a loop, despite the fact that it COULD be vectorized.
count5 = 0;
for i = 1:2^10
count5 = count5 + ~isempty(strfind(sequences(i,:),ones(1,5)));
end
count5
count5/2^10
Look carefully at the inner line of code there. It uses strfind to identify sequences that have a sub-sequence of length 5, that matches the string of interest. Since all you care about is that it finds a sub-sequence of length at least 5, this is adequate. Then the ~isempty call retiuurns true, when such a sequence was found.
2 comentarios
John D'Errico
el 13 de Oct. de 2022
Editada: John D'Errico
el 13 de Oct. de 2022
Oops. I missed the word exactly. But that is easy enough to fix. Appending a 0 at each end, and then searching for the subsequence [0 1 1 1 1 1 0] is the solution that I would use. Since I see Torsten does exactly that, he is correct.
Image Analyst
el 14 de Oct. de 2022
Yet another way (Monte Carlo)
numExperiments = 100000
numTosses = 10
tosses = randi([0, 1], numExperiments, numTosses);
maxLength = zeros(numExperiments, 1);
% Find the lengths of each run of 1's
for k = 1 : numExperiments
if nnz(tosses(k, :)) == 0
continue;
else
props = regionprops(logical(tosses(k, :)), 'Area');
maxLength(k) = max([props.Area]);
end
end
fraction5 = sum(maxLength == 5) / numExperiments
fraction5 =
0.06296
0 comentarios
Ver también
Categorías
Más información sobre Monte Carlo Analysis 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!