How to neatly alternate between two different statements at different intervals

2 visualizaciones (últimos 30 días)
I'm struggling to figure out how I can use a for loop to execute two alternating statements at different intervals.
So in the interval ii=1:100, I want to execute one line of code when ii=[1:10, 21:30, 41:50, 61:70, 81:90] and the other line of code to execute when ii=[11:20, 31:40, 51:60, 71:80 91:100].
The best solution I've come up with is doing ten separate for loops one after the other... but I'm assuming this is the least efficient method to do this.
For context, the output here is an image being constantly changed from one state to another, back and forth, clearing the figure and showing the image at every interval.
Any help would be greatly appreciated - thank you!!

Respuesta aceptada

dpb
dpb el 15 de Ag. de 2019
counter=0;
Limit=10;
for i=1:100
counter=counter+1;
if counter<=Limit
disp('A')
else
disp('B')
counter=0;
end
end
  2 comentarios
George Simmonds
George Simmonds el 15 de Ag. de 2019
Thanks! This got me right on track with a few edits! Thanks for your help dpb
dpb
dpb el 16 de Ag. de 2019
No problem, glad to help...sometimes we overthink things too much and miss the obvious.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by