how to exit for loop

287 ビュー (過去 30 日間)
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 8 月 10 日
コメント済み: Tong Zhao 2022 年 6 月 26 日
Hi, I have the following code:
for m=1:10
for n=1:sz(2)
if(Isingle(m,n)==1)
index1=[m n];
break;
end
end
end
I need to exit from the entire for loop i.e. for m=1:10 and for n=1:sz(2) when any index value is found, i don't know how to do that. can any body help?
Thanks

採用された回答

Andrei Bobrov
Andrei Bobrov 2011 年 8 月 10 日
[i1 j1] = find(Isingle' == 1, 1, 'first')
OR with loops
for m=1:10
for n=1:sz(2)
if(Isingle(m,n)==1)
index1=[m n];
return
end
end
end

その他の回答 (1 件)

Friedrich
Friedrich 2011 年 8 月 10 日
Hi,
I think you have to use a flag
flag = 0
for m=1:10
for n=1:sz(2)
if(Isingle(m,n)==1)
index1=[m n];
flag = 1
break;
end
end
if flag == 1
break;
end
end
  2 件のコメント
Friedrich
Friedrich 2011 年 8 月 10 日
Thanks, your are right. fixed it.
Tong Zhao
Tong Zhao 2022 年 6 月 26 日
Thanks from 2022, very educational.

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

カテゴリ

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