How to neatly alternate between two different statements at different intervals

2 ビュー (過去 30 日間)
George Simmonds
George Simmonds 2019 年 8 月 15 日
コメント済み: dpb 2019 年 8 月 16 日
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 Center および File ExchangeLoops 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