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

2 vues (au cours des 30 derniers jours)
Navodita Das
Navodita Das le 29 Mai 2020
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

Réponse acceptée

Bjorn Gustavsson
Bjorn Gustavsson le 29 Mai 2020
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 commentaire
Navodita Das
Navodita Das le 30 Mai 2020
Okay, Thanks alot. I got my mistake. Thank you for helping.

Connectez-vous pour commenter.

Plus de réponses (2)

Adam Danz
Adam Danz le 29 Mai 2020
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 le 30 Mai 2020
if true
% code
end

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by