MATLAB Answers

will99
0

How top stop for loop if value is reached

Asked by will99
on 18 Apr 2019
Latest activity Edited by James Tursa
on 19 Apr 2019
Z is 2000 by 2000 matrix filled with values
I have this for loop I want it to stop when there is no change in the matrix X
so if by the 200 itration there is not a huge change in the vector 199 and 200 stop the loop
for c = 1:2000
X(c+1,:) = X(c,:)*Z;
end

  0 Comments

Sign in to comment.

Products


Release

R2019a

1 Answer

Answer by James Tursa
on 18 Apr 2019
Edited by James Tursa
on 19 Apr 2019
 Accepted Answer

if( norm(X(c+1,:)-X(c,:)) < some_tolerance )
break;
end
or perhaps
if( all(abs(X(c+1,:)-X(c,:))) < some_tolerance )
break;
end
In the second example, some_tolerance could be a vector if you wanted to apply different tolerances to different elements

  0 Comments

Sign in to comment.