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 回表示 (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by