简体   繁体   中英

Matlab: modify each cube of 3D cell matrix

I have used mat2cell to create a 12 x 13 x 5 cell matrix where each cell contains a 29 x 29 x 29 cube. How do I generate a new 3D cell matrix containing only the central 21 x 21 x 21 cube of each 29 x 29 x 29 cube? ie so the new matrix is still 12 x 13 x 5 but each one contains a 21 x 21 x 21 of central voxels of original cube?

To crop an indivual block, you can use a simple indexing expression:

c=4 % cut of c rows on each side.
for ix=1:numel(X)
   X{ix}=X{ix}(1+c:end-c,1+c:end-c,1+c:end-c);
end

If you are aiming for a solution with a better Performance, I recommend switching to multi dimensional matrices:

c=4
Y=reshape(X,29,12,29,13,29,5);
%now the first block is squeeze(Y(:,1,:,1,:,1))
%now cut to smaller blocks:
Y=Y(1+c:end-c,:,1+c:end-c,:,1+c:end-c,:);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM