Getting the mean of when a random process hits a desired number of successes

6 views (last 30 days)
Hi, I have an Nxn matrix, where usually N = 1000 and n = 20. I have written a code in order to make this matrix show me when the processes hits a certain number of successes, namely 10. Each line is one realization of the process. I need to pick the first time it hits 10, register in which trial it occurred, and then make a simple mean from the N lines of the matrix. (My code is attached to this question). Thanks in advance.
clear all;
N = 1000;
p = 0.4;
n = 20;
xn = rand(N,n);
% Tornando xr uma matriz de variavel indicadora
xn(xn >= 1-p) = 1;
xn(xn < 1-p) = 0;
% Tornando xr um processo estocastico Sa(n)
for i = 2:1:n
xn(:,i) = xn(:,i) + xn(:,i-1);
end
xn(xn ~= 10) = 0;
xn(xn == 10) = 1;
This is what I was trying, it seemed to be working outside the for loop though,
contador = 0;
for i = 1:N
contador = contador + find(xn(i,:),1);
end
mean = contador/N;

Answers (1)

Walter Roberson
Walter Roberson on 12 Apr 2014
xn = cumsum( rand(N,n) >= 1-p, 2 ) == 10;
mean( arrayfun(@(K) find(xn(K,:),1), 1:N) )
But is it certain that 10 will be hit on every line?
  8 Comments
Philippe
Philippe on 12 Apr 2014
Edited: Walter Roberson on 12 Apr 2014
Well, I was trying to be efficient by using the MATLAB style, but since that wasn't going anywhere, I made it with a more traditional programming. Here is what I came with:
indicador = 0;
N_indicador = 0;
for i = 1:N
counter = 0;
j = 1;
while counter == 0 && j < 21
if xn(i,j) == 1
indicador = indicador + j;
N_indicador = N_indicador + 1;
counter = 1;
else
j = j + 1;
end
end
end
mean = indicador/N_indicador

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!