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 次查看(过去 30 天)
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

回答(1 个)

Rahul Kalampattel
Rahul Kalampattel 2017-3-12
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

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by