在 if 语句中将值与 Matlab 中的空集 [] 进行比较

Comparing values to the empty set [] in Matlab in an if statement

我想在比较结果为 []TRUE 时执行操作。

例如,假设比较是 x > 1,其中 x 可以是以下两者。

x = 2
x = []

如果 x > 1 给出 []TRUE,我想显示 'yes'。

我可以

if x <= 1
    disp 'no'
else 
    disp 'yes'
end

但是有没有一种直接的方法可以在不取反比较运算符的情况下做到这一点?

针对第一个答案进行编辑:我想避免使用 isempty 运算符。

if isempty(x) || x > 1
    disp 'yes'
else
    disp 'no'
end

你可以在比较时调用all函数

if all(x > 1)
    disp 'yes'
else
    disp 'no'
end

那是因为

>> all([])
ans =
     1
>> all(1)
ans =
     1

您可能需要添加评论,说明为什么在该代码的未来 reader 的标量比较中使用 all(即使那只是您)。

您需要针对 isempty 和我们的比较进行测试,但您在评论中表示您不愿意直接为许多变量键入 isempty。这个怎么样?那么您只需输入一次 isempty

mycomp = @(x) isempty(x) || x>1;

if mycomp(x)
    disp('yes')
else
    disp('no')
end