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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by