Cutting a for loop when a certain value is repeated
    3 views (last 30 days)
  
       Show older comments
    
Is there any way of cutting a for loop when a certain value is reached? I have a loop in the form of
for k = 1:numel(filelist)
this loop produces a value of X at the end of every loop which is then stored in a text file. I was just wondering whether there is any way that the for loop can be cut when this value of X stops increasing/ stays the same?
I thought something maybe along the lines of
if X(k-1)==X(k)
    break
end
but this doesn't work. I guess its because the loop overwrites X. I was just wondering whether anyone had any ideas? Thanks in advance!
0 Comments
Answers (2)
  Sean de Wolski
      
      
 on 16 Apr 2014
        Sure, pseudocode:
xtemp = nan;
for ii = 1:1000
 x = whatever
 do stuff
 if x==xtemp
     break
 else
    xtemp = x;
 end
end
4 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!

