How top stop for loop if value is reached
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
will99
el 18 de Abr. de 2019
Editada: James Tursa
el 19 de Abr. de 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 comentarios
Respuesta aceptada
James Tursa
el 18 de Abr. de 2019
Editada: James Tursa
el 19 de Abr. de 2019
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!