How can I break from an inner loop but still continue the outer loop?

38 ビュー (過去 30 日間)
AAQIB PEERZADA
AAQIB PEERZADA 2015 年 11 月 29 日
コメント済み: AAQIB PEERZADA 2015 年 11 月 30 日
I am writing a code where I am extrapolating a set of data by straight lines. It involves finding slope and intercept and extrapolating the line. I am able to draw a family of lines with different slopes and y-intercepts, however the problem also involves estimating a certain parameter for all the lines. Here is a section of my code where I need help:
for t=1:1:473;
for Lfn=1.0005:0.0005:5;
p=p+1;
L=L+1;
E1(t,p)=abs(0-Yextrap(t,p));
while L>1
if E1(t,p)>E1(t,(p-1))
disp(p-1)
ECLF1=1.0005+0.0005*(p-1);
end
L=L-1;
end
end
end
Here when the condition following the "if statement" is met, i need to execute the two lines after the if statement and then break from the inner while and for loops, but continue the outer most for loop? Any help would be appreciated.

回答 (1 件)

James Tursa
James Tursa 2015 年 11 月 29 日
One way is to use an auxiliary variable. E.g.,
for t=1:1:473;
breakinner = false;
for Lfn=1.0005:0.0005:5;
p=p+1;
L=L+1;
E1(t,p)=abs(0-Yextrap(t,p));
while L>1
if E1(t,p)>E1(t,(p-1))
disp(p-1)
ECLF1=1.0005+0.0005*(p-1);
breakinner = true;
break;
end
L = L - 1;
end
if( breakinner )
break;
end
end
end
  1 件のコメント
AAQIB PEERZADA
AAQIB PEERZADA 2015 年 11 月 30 日
Thank you for the help. I found another way to solve my problem by changing the if condition. That way i didn't have to get out of the loop and still get my result. But your code also works. Thanks.

サインインしてコメントする。

カテゴリ

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