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

조회 수: 10 (최근 30일)
George Papas
George Papas 2016년 9월 12일
댓글: George Papas 2016년 9월 12일
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.
George Papas
George Papas 2016년 9월 12일
Thanks Stephen.

댓글을 달려면 로그인하십시오.

추가 답변 (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.
  댓글 수: 1
George Papas
George Papas 2016년 9월 12일
Thank you Guillaume, that's true. It works very well and it is really concise indeed!

댓글을 달려면 로그인하십시오.

카테고리

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