离散化连续数学方程式

时间:2019-05-14 15:14:31

标签: matlab math

我想实现一个代码,将大小为(bs x bs x bs)的多维数据集中的信息改组,以便将多维数据集(x,y,z)中的每个元素唯一地映射到(x_new,y_new,z )。但是代码无法正常工作。

enter image description here

所附图像是我试图离散化和实现的代码的实际数学定义。原始的数学方程式适用于单位立方,而我的立方具有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

1 个答案:

答案 0 :(得分:-1)

您的某些情况不清楚:

  • 为什么您要检查1<=row是否行总是>=1
  • 为什么要使用模数? (由@Welbog指出)
  • ...

但是要回答您的问题,我建议您使用逻辑索引,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      = ...