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

3 visualizaciones (últimos 30 días)
George Papas
George Papas el 12 de Sept. de 2016
Comentada: George Papas el 12 de Sept. de 2016
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.

Respuesta aceptada

KSSV
KSSV el 12 de Sept. de 2016
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 comentarios
Stephen23
Stephen23 el 12 de Sept. de 2016
Editada: Stephen23 el 12 de Sept. de 2016
There is no point in using a loop to solve this task. See Guillaume's answer for a much simpler solution.

Iniciar sesión para comentar.

Más respuestas (1)

Guillaume
Guillaume el 12 de Sept. de 2016
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 comentario
George Papas
George Papas el 12 de Sept. de 2016
Thank you Guillaume, that's true. It works very well and it is really concise indeed!

Iniciar sesión para comentar.

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by