How to neatly alternate between two different statements at different intervals

2 Ansichten (letzte 30 Tage)
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!!

Akzeptierte Antwort

dpb
dpb am 15 Aug. 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 Kommentare
George Simmonds
George Simmonds am 15 Aug. 2019
Thanks! This got me right on track with a few edits! Thanks for your help dpb
dpb
dpb am 16 Aug. 2019
No problem, glad to help...sometimes we overthink things too much and miss the obvious.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by