如果向量只包含零,如何检查MATLAB?

Luc*_*cas 13 matlab

什么是"MATLAB方式"来检查向量是否只包含零,以便它将被评估为标量而不是向量.如果我运行此代码:

vector = zeros(1,10)

%the "1" represents a function that returns a scalar
if 1 && vector == 0   %this comparision won't work
    'success'
end
Run Code Online (Sandbox Code Playgroud)

我收到错误:

??? 操作数到|| 和&&运算符必须可转换为逻辑标量值.

pto*_*ato 22

用途all:

vector = zeros(1,10)
if 1 && all(vector == 0)   %this comparision will work
    'success'
end
Run Code Online (Sandbox Code Playgroud)


Ric*_*ton 14

正如ptomato所暗示的那样false,由于零处理的方式相同,所以不需要使用. 是检查零值的"MATLAB方式".vector == 0~any(vector)

if 1 && ~any(vector)   
    'success'
end
Run Code Online (Sandbox Code Playgroud)

将问题扩展到数组,您必须使用

array = zeros(5);
if 1 && ~any(array(:))
    'success'
end
Run Code Online (Sandbox Code Playgroud)