matlab set loop conditons question

2 ビュー (過去 30 日間)
peter huang
peter huang 2022 年 5 月 4 日
回答済み: Voss 2022 年 5 月 4 日
When multiple conditions are set in the loop, when encountering an item that satisfies the repeated conditions, how to determine the level of the loop condition
If I have three conditions
for k = 1:6
if mod(k,3)=0 (Common multiples of three) [condition:1]
When this condition is encountered, it will be executed first, and the other two conditions will not be executed.
elseif mod(k,2)==0 (even)(2) [condition:2]
not Common multiples of three execute this conditional instruction
else mod(k,2)==1(odd number) [condition:3]
not Common multiples of three execute this conditional instruction
end
when k=3 condition 1 and 3 repeat
when k =6 condition 1 and 2 repeat
what can i do to aviod this from happening

採用された回答

Voss
Voss 2022 年 5 月 4 日
Break it into two parts maybe (but it's not clear what you want to happen when k is 1, 2, 4, or 5).
for k = 1:6
if mod(k,3) == 0 % (Common multiples of three) [condition:1]
end
if mod(k,2) == 0 % (even)(2) [condition:2]
else % mod(k,2)==1(odd number) [condition:3]
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by