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