if 0 < x < 1 in matlab

In MATLAB, you can use the logical AND operator & to check if a value falls within a certain range. For example, to test if a variable x is between 0 and 1 exclusive, you could use the following condition:

main.m
if 0 < x & x < 1
    disp('x is between 0 and 1')
else
    disp('x is not between 0 and 1')
end
96 chars
6 lines

Alternatively, you could use the double-ampersand && operator for a short-circuiting evaluation of the same condition:

main.m
if x > 0 && x < 1
    disp('x is between 0 and 1')
else
    disp('x is not between 0 and 1')
end
97 chars
6 lines

Notice that using && can sometimes be faster, because it stops evaluating the condition as soon as it decides the truth value (i.e., if x is less than or equal to 0, it won't bother checking if it's less than 1).

Note that if x can take on non-finite or complex values, you may need to use the isnan or isinf functions to check for those cases before applying the range condition.

gistlibby LogSnag