if i want to stop infinite while loop after 30 iteration, so how can i use it in my matlab code?

2 views (last 30 days)
if i want to stop infinite while loop after 30 iteration, so how can i use it in my matlab code?

Accepted Answer

Image Analyst
Image Analyst on 11 Feb 2013
Edited: Image Analyst on 11 Feb 2013
loopCounter = 1
while true % Infinite loop
% Insert code here....
% At bottom of loop, break out of loop if counter = 30
if loopCounter >= 30
break;
end
% Done with code so now increment loop counter.
loopCounter = loopCounter + 1;
end
However I suggest that it's better to use a "for" loop than an infinite while loop that you have to break out of.
for k = 1:30
% Your code
end
Or you could have a non-infinite while loop where you test for the loopCounter on the "while" line, as you're normally expected to do. Any reason why you're not doing either of those methods?
  1 Comment
swati chauhan
swati chauhan on 12 Feb 2013
i was using it to compare two images that one image is 90% similar to another for this i provided a infinite loop now i m confusing in that two image compared either their pixels match count or histogram so if i choose first one then how it should be written? if i write
for i1=(1:m)
for j1=(1:n)
if image1(i1,j1)==Image2(i1,j1);
noisyRSMatchedCount = noisyRSMatchedCount + 1;
S(k)=noisyRSMatchedCount/(m*n);
end
end
end
where s(k)giving matched pixels but problem is that how it will be match 90% , please help me

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 11 Feb 2013
Edited: Azzi Abdelmalek on 11 Feb 2013
test=0
while condition & test<=30
%your code
test=test+1
end
  3 Comments
Image Analyst
Image Analyst on 11 Feb 2013
Edited: Image Analyst on 11 Feb 2013
It's good to avoid infinite loops. You should always have a failsafe in there, like bail out if the number of iterations exceeds some number that you know is abnormally high, which means something went wrong. Otherwise it's possible for your program to hang the computer.
swati chauhan
swati chauhan on 12 Feb 2013
i was using it to compare two images that one image is 90% similar to another for this i provided a infinite loop now i m confusing in that two image compared either their pixels match count or histogram so if i choose first one then how it should be written? if i write
for i1=(1:m)
for j1=(1:n)
if image1(i1,j1)==Image2(i1,j1);
noisyRSMatchedCount = noisyRSMatchedCount + 1;
S(k)=noisyRSMatchedCount/(m*n);
end
end
end
where s(k)giving matched pixels but problem is that how it will be match 90% , please help me

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!