Getting the mean of when a random process hits a desired number of successes
6 views (last 30 days)
Show older comments
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;
0 Comments
Answers (1)
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
Walter Roberson
on 12 Apr 2014
My code does assume 10 will be hit on every line.
xn = xn(any(xn,2),:);
See Also
Categories
Find more on Logical 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!