将3D矩阵划分为多维数据集

时间:2015-05-28 14:51:00

标签: matlab matrix multidimensional-array

我有一个3D图像表示为3D矩阵。

现在我想将图像分割成大小为 k x k x k 的多维数据集,即在图像上有一个滑动立方体。

对于每个立方体,我想对其中的所有体素进行操作。

当然,我可以使用3个嵌套循环来完成它,但事实并非如此简单,因为当多维数据集移动时,多个维度会发生变化。

你会怎么做?如果有人能提供一些示例代码我会很高兴。我是在MATLAB中做的。

1 个答案:

答案 0 :(得分:0)

如果你想将立方体放在单元格数组的单元格中,你可以做这样的事情。

%This is to randomly decide the size the of images so you can see it's general
n1 = randi(100);
n2 = randi(100);
n3 = randi(100);

%Make a substitute image
M = rand(n1, n2, n3);

%Then randomly choose a cube side length
k = randi(min([n1, n2, n3]));

%%% This is where your real code would start %%%
%%% Input your original image M and your voxel size k %%%
%%% If on the front end you ensure that you match you only need the last line but, I put the preprocessing in as a precaution. %%%

% Get the size of the matrix
[n1, n2, n3] = size(M);

%Pad the images to make them equally divide into your cube
MPrime = zeros(k - mod(n1, k) + n1, k - mod(n2, k) + n2, k - mod(n3, k) + n3);

%Put the image into the padded storage
MPrime(1:n1, 1:n2, 1:n3) = M;

%Split up the voxels into their cubes
MBoxed = mat2cell(MPrime, k*ones(size(MPrime, 1)/k, 1), k*ones(size(MPrime, 2)/k, 1), k*ones(size(MPrime, 3)/k, 1));