How top stop for loop if value is reached

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

 채택된 답변

James Tursa
James Tursa 2019년 4월 18일
편집: James Tursa 2019년 4월 19일

0 개 추천

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개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2019년 4월 18일

편집:

2019년 4월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by