MATLAB: How i break nested for loop

for loopprogramming

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

Best Answer

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.