MATLAB: Please help me with coding up “-5 < x < 3"

logical operation

How can I write this equation in MATLAB?

Best Answer

It depends on what "x" is and what you want as output. Please post more details, because letting the readers guess the intention is not efficient.
Perhaps you want:
if -5 < x && x < 3 % Equivalent: and(-5 < x, x < 3)
or
Mask = and(-5 < x, x < 3); % If x is an array
Maybe "x" is a symbolic variable or you want to use this as restriction for an optimization? Or you want to display the formula formatted by LaTeX in a title?
If you write "-3<x<5" directly, the result may be unexpected: Matlab evaluates this from left to right. "-3<x" is either TRUE or FALSE. In the next step you get "true<5" or "false<5", and because the logical values are treated as numerical 0 and 1, both relations are true independent from the value of x.