how to exit a for loop if a condition is true?!
    46 次查看(过去 30 天)
  
       显示 更早的评论
    
hello! I would like to exit a for lopp is a condition is true but my code doesn't seem to work, could you help me to figure out where is the problem?! Thank you best regards!
    a= [ 1 2 3 5 8 6 8  8 2 8 2 8 2 8 2 1 nan 45 56 89];
    for i= 1:length(a)
    indx1 = find(isnan(a));
    if ~isempty (indx1)
       L = i ;
       return
    end
    end
回答(2 个)
  KSSV
      
      
 2017-5-16
        a= [ 1 2 3 5 8 6 8  8 2 8 2 8 2 8 2 1 nan 45 56 89];
    for i= 1:length(a)
    indx1 = find(isnan(a));
    if ~isempty (indx1)
       L = i ;
       break
    end
    end
  Walter Roberson
      
      
 2017-5-16
        L = find(isnan(a), 1, 'first');
with no loop.
You are testing the same vector of values each time, all of a, so your result would always be either 1 or not found.
0 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


