How to optimize condition code?

1 回表示 (過去 30 日間)
galaxy
galaxy 2022 年 9 月 14 日
コメント済み: galaxy 2022 年 9 月 14 日
Hi all,
I have example for check value in for loop as below
for cnt=1:10
if cnt == 1
if a(cnt) == 0
statement A;
else
statement B;
end
else %% if cnt >= 1
if a(cnt) == 0 || a(cnt-1) == 1
statement A;
else
statement B;
end
end
end
You can see that some lines are dupplicate. Do anyone can optimize condition in loop ?
for cnt=1:10
if new_condition ???
statement A;
else
statement B;
end
end

採用された回答

Chunru
Chunru 2022 年 9 月 14 日
for cnt=1:10
if (cnt == 1 && a(cnt) == 0) || (cnt>1 && (a(cnt) == 0 || a(cnt-1) == 1))
statement A;
else
statement B;
end
end
  1 件のコメント
galaxy
galaxy 2022 年 9 月 14 日
Thank you so much

サインインしてコメントする。

その他の回答 (0 件)

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by