I am running a for loop for my program. however, I want to run the for loop until i have 10 non-zero values in the matrix A.

1 visualización (últimos 30 días)
In the following program I want "nreps" equal to "c(A~=0, 2) = 10". How can I obtain that?
function [iseed, time] = problem4 (iseed, lambda, nreps, seed)
time = 0;
for i = 1: nreps
[iseed, u] =u16807d(iseed);
time= time-(log(1-u)/lambda);
[seed, x] = mrg32k3a(seed);
if x <= (3+(4/(time+1)))/lambda
A(i) = time;
end
end
c = sum(A~=0, 2)
B = nonzeros (A)
disp (A)
end

Respuestas (1)

Rahul Kalampattel
Rahul Kalampattel el 12 de Mzo. de 2017
The simplest solution would be to change the for loop to a while loop. Just calculate c at each iteration, and once c==10, the loop will stop.
function [iseed, time] = problem4 (iseed, lambda, nreps, seed)
time = 0;
c=0;
i=1;
while c<10
[iseed, u] =u16807d(iseed);
time= time-(log(1-u)/lambda);
[seed, x] = mrg32k3a(seed);
if x <= (3+(4/(time+1)))/lambda
A(i) = time;
end
c = sum(A~=0, 2);
i = i+1;
end
B = nonzeros (A);
disp (A)
end

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by