How to neatly alternate between two different statements at different intervals

조회 수: 1 (최근 30일)
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!!

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by