Is there any other way to set range in if- else statement?

조회 수: 2 (최근 30일)
Navodita Das
Navodita Das 2020년 5월 29일
댓글: Soundarya Natarajan 2020년 5월 30일
Hello, please help me with this code.
Why condition is not obeyed? or maybe something's missing?
Code:
m=45
if 0<m<2
disp('blow')
end
Result:
blow

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2020년 5월 29일
Your expression in the condition is not what you think it is. You set m to 45. Matlab interprets your test-condition thisly:
(0<m)<2
and since m is larger than zero (0<m) returns 1 (datatype logical), the next comparison is 1<2 which returns true again.
What you need to test is that 0<m and that m<2. Try that. Look for the help and documentation for and.
HTH
  댓글 수: 1
Navodita Das
Navodita Das 2020년 5월 30일
Okay, Thanks alot. I got my mistake. Thank you for helping.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Adam Danz
Adam Danz 2020년 5월 29일
Explanation of the error
0<45<2 is the interpretted as (0<45)<2 or, simplifed to 1<2 where 1 is TRUE.
Solution
if 0<m && m<2

Soundarya Natarajan
Soundarya Natarajan 2020년 5월 30일
if true
% code
end

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by