简体   繁体   中英

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. 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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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