How to use a while loop with irregular step?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
bh dhouha
el 8 de Ag. de 2015
Comentada: bh dhouha
el 8 de Ag. de 2015
i would like to use a "while" loop ( while i<20) but with an irregular step. For example it goes this way : i=1,2,3, 5,7,9,12,15,18....it means it starts by a 1-step then by 2 then by a 3-step.
0 comentarios
Respuesta aceptada
Walter Roberson
el 8 de Ag. de 2015
while loops do not have steps. "for" loops can have steps but also can be irregular:
for i = [1,2,3, 5,7,9,12,15,18]
5 comentarios
Más respuestas (1)
David Young
el 8 de Ag. de 2015
Editada: David Young
el 8 de Ag. de 2015
You can do it with a for loop very simply:
for i = [1,2,3, 5,7,9, 12,15,18]
% < process using i >
end
By the way, some people take the view that it is unwise to use i or j as ordinary variables because of potential confusion with the imaginary unit, so it might be better to use ii or k or something.
2 comentarios
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!