Extract 3D cells with nonzero elements, from a 3D cell array.

15 次查看(过去 30 天)
Hi guys! I have binary masks saved in 3D arrays (e.g. Mask(:,:,40), see attached Matlab file) and I want to extract only the 3D cell arrays which contain nonzero elements in a sequential order (e.g. if these are 20, then NMask(:,:,20)). Any ideas would be much appreciated.

采纳的回答

KSSV
KSSV 2016-9-12
clc; clear all ;
load Mask.mat ;
k = Combinedmask ;
[m,n,p] = size(k) ;
count = 0 ;
for i = 1:p
ki = k(:,:,i) ;
if sum(any(ki))~=0
count = count+1 ;
iwant{count} = ki ;
end
end
  3 个评论
Stephen23
Stephen23 2016-9-12
编辑:Stephen23 2016-9-12
There is no point in using a loop to solve this task. See Guillaume's answer for a much simpler solution.

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2016-9-12
Well, if you want nice and concise:
filteredmask = Combinedmask(:, :, any(any(Combinedmask, 1), 2))
No need for cell arrays, loops, ifs, etc., just one line.

类别

Help CenterFile Exchange 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by