Find non zero values in a 4-D array

1 view (last 30 days)
Andrew
Andrew on 23 Sep 2014
Answered: Adam on 23 Sep 2014
I have a 4D array and I'd like to figure out when under this specific variable are the values non zero.
example:
Array(:,:,:,i) where i goes from 1:6. I need to figure out when the values are non zero and then extract the value of i to be assigned to another variable.
I attempted to work with any(), but I'm having difficulty with that loop.
Example loop:
for i=1:6
if any(Array(:,:,:,i)) = 1
p = i
end
end

Answers (2)

Image Analyst
Image Analyst on 23 Sep 2014
Get the logical indices where it's non-zero like this
logicalIndices = Array ~= 0;
I'm not sure what your plans were for that loop you wrote. You're not checking if it's non-zero, you're just checking if each 3D volume has a 1 anywhere it and then setting p equal to the volume index you're checking, but possibly overwriting it on each iteration. Does not make sense to me.

Adam
Adam on 23 Sep 2014
[x,y,z,w] = ind2sub( size(array), find( ~array ) );
will give you 4-d indices of the zero elements if that helps. All the w indices in that result would be the i's you seem to want and the x,y,z at corresponding indices will give the locations in those dimensions if you also need those.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!