how to exit for loop
165 views (last 30 days)
Show older comments
Mohammad Golam Kibria
on 10 Aug 2011
Commented: Tong Zhao
on 26 Jun 2022
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
0 Comments
Accepted Answer
Andrei Bobrov
on 10 Aug 2011
[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
0 Comments
More Answers (1)
Friedrich
on 10 Aug 2011
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 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!