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.

2 Ansichten (letzte 30 Tage)
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

Antworten (1)

Rahul Kalampattel
Rahul Kalampattel am 12 Mär. 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

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by