MATLAB: Matlab function

I have write a code to find the first pixel that not equal to zero and store the pixel position to M and N respectively as follow:
j=1;
while (j>=1&&j<=size(I,1) )
for i=1:size(I,2)
if I(i,j) >= 0
M=j
N=i
break;
else
end
end
j=j+1;
end
where I is the image;
when the code find the first pixel that not equal to zeros,however , the loop can not stop, the break seems do not help to stop the loop;
Can you please help me on this?

Best Answer

The break is breaking out of the "for", but then you are still within the "while". You need to set a flag before you break and test it after the "for" loop code, using break when the flag is set.
Related Question