フィルターのクリア

how i break nested for loop

38 ビュー (過去 30 日間)
m. muner
m. muner 2016 年 4 月 10 日
コメント済み: m. muner 2016 年 4 月 11 日
hello i have three loops and two conditions and flag and i'm trying to use break statement to break the loop after the second condition true but what i get two variables having same value which is impossible what wrong
temp=0;cov=0;sov=0;
for I=1:30
for J=1:30
for N=1:x
dis=sqrt(((I-test_ary(N,2)).^2)+((J-test_ary(N,1)).^2));
if(dis<=r)
if (temp==0)
cov=cov+1;
temp=1;
else
if(temp==1)
sov=sov+1;
temp=0;
break
end
end
end
end
end
end
  2 件のコメント
dpb
dpb 2016 年 4 月 10 日
What is the expected result you're looking for? The first break will only terminate the innermost loop (on N) so the outer loops will still run to completion (which, of course, will start the innermost loop over again each pass). And, of course, since you reset temp in the else clause, the cov accumulator may increment again.
A description of the objective you're trying to achieve might help.
m. muner
m. muner 2016 年 4 月 11 日
well i have gird of small squares and circles with radius distributed in the area and I'm measuring the distance between each circle and square if the distance less than r the cov increase if two circles have distance less than r with the same square the sov increase and the loop is braked so sov should be smaller than cov not equal

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

採用された回答

Image Analyst
Image Analyst 2016 年 4 月 10 日
Set a flag:
finishNow = false; % Call this before the loop to initialize.
Then in the loop:
finishNow = true;
Right before the "ends" for i and j, break if the flag is set:
if finishNow
break
end
end % of i or j loop
You will need that twice - once for the i loop and once for the j loop.

その他の回答 (0 件)

カテゴリ

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