简体   繁体   English

如何在MATLAB中检查4D矩阵中的非零值?

[英]How to check for non-zero values in 4D matrix in MATLAB?

I am stuck with this simple matrix operation. 我被这个简单的矩阵运算所困扰。 I have the 4D matrix called F, class double, with size(F)= 10 345 345 14. (I am not quite sure if I should have converted to logical) It contains 0s and 1s and it represents a mask after some operations to check neighbouring. 我有一个称为F的4D矩阵,类为double,大小为(F)= 10 345 34514。(我不太确定我是否应该转换为逻辑)它包含0和1,并且在执行一些操作后表示一个掩码检查邻居。 I need to see for each 'object' (first dimension, 1:10), if there are any non-zero values and create a logical array Con with size(Con)=10 that will have 1 if there are non-zero values, or 0 if all values are 0. I tried many different approaches, I am not sure how many 'any' I should put and if I need to apply squeeze every time. 我需要查看每个“对象”(第一维,1:10)是否存在任何非零值,并创建一个逻辑数组Con,其大小为(Con)= 10,如果存在非零值则为1 ,如果所有值均为0,则返回0。 This is my code: 这是我的代码:

[m,n,o,p]=size(F);

for ob=1:m         %for each object
    A=F(ob,:,:,:);
    A=squeeze(A);
    if any(A(:))
       Con(ob)=1;
    else
       Con(ob)=0;
    end

end

Any help would be greatly appreciated, thanks! 任何帮助将不胜感激,谢谢!

Ziggy. 齐格。

尝试这个 -

Con = any(reshape(F,m,[]),2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM