set a if loop within a for loop

hi
I want to set an if loop within for loops
My idea is if the constraint satified then stop the for (j) loop and profit (i,j) does not exist; otherwise continue the loop and find the solution
but my code doesnt work well: it cannot stop the for loop when the if loop is satisfied
for i=1:numel(k)
for j=1:numel(lambda)
if lambda(j)/(mu*k(i)) >= 1
return
else
n = (0:(k(i)-1));
W = 1/(sum((a).^n./factorial(n))+a^k(i)/(factorial(k(i))*(1-a/k(i))));
profit(i,j) = lambda(j)*(p-w);
end
end
end

 Accepted Answer

KSSV
KSSV on 9 May 2017
Replace return with break.

5 Comments

yes thanks using break makes the corresponding profit(i,j) equal to zero but I want to set the value does not exist do you have any method??
Thanks a lot!!
but I want to set the value does not exist not clear..what value you want to set?
I mean no value... I want to set if the constraint is satisfied the corrreponding point does not exist
You can initialize profit to a NaN matrix. Where ever it is not calculated you will get NaN.
profit = NaN(numel(k),numel(lambda)) ;
it works. thanks!!!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 9 May 2017

Commented:

on 9 May 2017

Community Treasure Hunt

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

Start Hunting!