Matlab "if array" 功能
Matlab "if array" functionality
刚刚在我的一些旧代码中偶然发现了一个错误,我在其中检查数组是否为空我刚刚写道:
if my_array
...(do stuff)
end
而不是使用 isempty 或类似的东西。
我发现 "if my_array" returns 仅当数组确实为空或数组中的一个或多个组件为 0 时才为 0。
这是预期的吗?究竟是怎么回事? matlab是否对所有元素进行与运算?
非常感谢
这正是现在的行为 documented:
An evaluated expression is true when the result is nonempty
and contains all nonzero elements (logical or real numeric). Otherwise,
the expression is false.
刚刚在我的一些旧代码中偶然发现了一个错误,我在其中检查数组是否为空我刚刚写道:
if my_array
...(do stuff)
end
而不是使用 isempty 或类似的东西。
我发现 "if my_array" returns 仅当数组确实为空或数组中的一个或多个组件为 0 时才为 0。
这是预期的吗?究竟是怎么回事? matlab是否对所有元素进行与运算?
非常感谢
这正是现在的行为 documented:
An evaluated expression is true when the result is nonempty and contains all nonzero elements (logical or real numeric). Otherwise, the expression is false.