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

 Accepted Answer

Jan
Jan on 1 Oct 2017
Edited: Jan on 1 Oct 2017
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.

More Answers (0)

Asked:

on 1 Oct 2017

Edited:

on 1 Oct 2017

Community Treasure Hunt

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

Start Hunting!