如果3D数组至少有一个非零元素,如何检查matlab?

时间:2015-06-18 14:21:02

标签: arrays matlab 3d

我尝试使用下一个代码检查3D数组是否全部为零:

notAll_n0_GreaterThan_ni=1;

while notAll_n0_GreaterThan_ni 
    notAll_n0_GreaterThan_ni=0;        
    mask=(n0<ni);
    numDimensions=ndims(mask);
    for dim_ind=1:numDimensions
        if any(mask,dim_ind) 
            notAll_n0_GreaterThan_ni=1;
            break;
        end
    end   
    if notAll_n0_GreaterThan_ni 
        n0(mask)=n0(mask)+1;
    end
end

似乎我在代码中有错误,因为最后我得到了例子:n_0(11,3,69)= 21而ni(11,3,69)= 21.1556。 我无法找到错误。如果有人向我展示我错在哪里以及是否有更简单的方法来检查3D数组中是否存在非零元素,我会感激不尽。

2 个答案:

答案 0 :(得分:2)

import pywinauto app = pywinauto.Application.start('cmd.exe', wait_for_idle=False) app.Window_().TypeKeys('cmd.exe /?{ENTER}', with_spaces=True, pause=0.1) app.Window_().TypeKeys('{ENTER 7}', pause=0.1) 表示 n - 维数组。要检查它是否包含至少一个非零元素,只需使用

x

例如:

any(x(:))

其他更奇特的可能性包括:

>> x = zeros(2,3,4);
>> any(x(:))
ans =
     0
>> x(1,2,2) = 5;
>> any(x(:))
ans =
     1

sum(abs(x(:)))>0

答案 1 :(得分:1)

这就是你要找的东西 B = any(your_Array_name_here(:) ==0);无需循环

(:)将your_Array的元素转换为单个列向量,因此您可以在任何大小的数组上使用此类型的语句

我已经对此进行了测试并且可以正常运行

A = rand(3,7,5) * 5; B = any(A(:) ==0);