Evaluate Multiple Conditions in Expression
조회 수: 7 (최근 30일)
이전 댓글 표시
I've been meaning to simplify my code by combining multiple conditions into one IF statement.
This is what I'm currently using :
if IntX < 1
ah.Color=[1, 1, 1];
xlabel(ax, ' ');
return;
end
if IntX > Inner(3)
ah.Color=[1, 1, 1];
xlabel(ax, ' ');
return;
end
if IntY < 1
ah.Color=[1, 1, 1];
xlabel(ax, ' ');
return;
end
if IntY > Inner(4)
ah.Color=[1, 1, 1];
xlabel(ax, ' ');
return;
end
This is not elegant but it works. I experimented with this statement but I get an error :
if (IntX < 1) && (IntX > Inner(3)) && (IntY < 1) && (IntY > Inner(4))
ah.Color=[1, 1, 1];
xlabel(ax, ' ');
return;
end
댓글 수: 2
채택된 답변
the cyclist
2022년 1월 9일
if (IntX < 1) | (IntX > Inner(3)) | (IntY < 1) | (IntY > Inner(4))
ah.Color=[1, 1, 1];
xlabel(ax, ' ');
return;
end
댓글 수: 2
the cyclist
2022년 1월 11일
You have the gist right, but in the short-circuited OR operation, it will evaluate from left-to-right until it finds a TRUE condition, because then it knows the entire expression is true.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!