Find non zero values in a 4-D array
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Image Analyst
2014-9-23
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.
0 个评论
Adam
2014-9-23
[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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!