我想实现一个代码,将大小为(bs x bs x bs)的多维数据集中的信息改组,以便将多维数据集(x,y,z)中的每个元素唯一地映射到(x_new,y_new,z )。但是代码无法正常工作。
所附图像是我试图离散化和实现的代码的实际数学定义。原始的数学方程式适用于单位立方,而我的立方具有bs。有人可以看一下我的代码并暗示所认为的错误吗?
n=bs;
NEWI=zeros(bs,bs,bs);
for row= 1:bs
for col=1:bs
for height=1:bs
if ( 1<=row && row<=(n/2) && 1<=col && col<=(n/2) )
x_new= 2*(row-1) + 1;
y_new= 2*(col-1) + 1;
z=floor(0.25*(height-mod(height-1,2)))+1;
end
for
for
for
Now this is just the first line implementation of the equation given in the figure. But as we see the coorepondence of points
(1,1,1) goes to (1,1,1)
(1,1,2) goes to (1,1,1)
(1,1,3) goes to (1,1,1)
(1,1,4) goes to (1,1,1)
which clearly is not a unique mapping, whereas the function claims of giving unique images for every (x,y,z). So my question is clearly there has to be some adjustments to discretize this map. Can somebody suggest
答案 0 :(得分:-1)
您的某些情况不清楚:
1<=row
是否行总是>=1
?但是要回答您的问题,我建议您使用逻辑索引,meshgrid
将使您的代码更具可读性。这样一来,您就可以完成所有操作。
% size of your 3D cube
n = 5;
% With meshgrid we generate the 3D grid that we will use to create the logical index.
[x,y,z] = meshgrid(1:n,1:n,1:n);
% Create empty array
M = zeros(n,n,n);
%first condition
I = x<(n/2) & y<(n/2); %create a logical index according to the first condition
M(I) = 2*x(I) + 2*y(I) + z(I)/4; %generate the associated value
%second condition
I = ...