本人程序:
if 0<= a<= 8
x=0;
else
x=1;
end
结果总是不对,原来 0<= a<8 的写法是错的,而应该修改为:
if (0<=a) && (a<8)
x=0;
else
x=1;
end
官方文件:
Explanation
MATLAB does not support testing whether y
is in between x
and z
with this syntax.
Instead, MATLAB interprets x < y < z
as (x < y) < z
, comparing the logical result of x < y
(either 0
or 1
) to the value z
. Statements using this type of interval testing rarely yield useful results.
Suggested Action
Break the comparison, x < y < z
, into a series of binary comparisons. If all arguments are numeric scalars, use the form (x < y) && (y < z)
. Otherwise, use the form (x < y) & (y < z)
.