How top stop for loop if value is reached
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
James Tursa
2019-4-18
编辑:James Tursa
2019-4-19
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 个评论
更多回答(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!