CoinToss(Seq,N)
function CoinToss(Seq,N)
%COINTOSS
% Compute the estimated average number of coin tosses to get a given
% sequence.
%
% CoinToss(Seq,N)
%
%============================================================
==============
% INPUTS:
%
% Seq - Sequence (row vector) of integers ranging from 0 to 1
% (corresponding to either Tails or Heads).
%
% N - Number of Monte-Carlo Simulations
%
%============================================================
==============
% EXAMPLE:
% >> CoinToss([1,1,0,1],10^4))
%
% >> OUTPUT:
% THE SEQUENCE IS:
% 1 1 0 1
% The average number of tosses to get this sequence is: 18
% (Sample Mean= 18.27 standard deviation= 0.15))
%
M=length(Seq);
C=rand(M,N)>.5;
Count=zeros(N,1);
for j=1:N
c=M;
while min(C(:,j)==Seq')==0
C(1:M-1,j)=C(2:M,j);
C(M,j)=rand(1,1)>.5;
c=c+1;
end
Count(j)=c;
end
Mean=mean(Count);