【问题标题】:Rotating elements in a MATLAB multidimensional array?旋转 MATLAB 多维数组中的元素?
【发布时间】:2014-11-23 03:24:45
【问题描述】:

我有某种形式的圆柱线源,我用它来概括 3D 内核;我通过首先生成一个简单的 2D 内核来做到这一点,如下所示:

A = 625;
B =  25;

%2D kernal 
grid2d = zeros(101,101);
c = 51; %centre

for m = 1:101
    for n = 1:101

   r(m,n) = sqrt((m - c).^2 + (n - c).^2); %distance of point on grid to centre

   %Populating the grid as a kernal 

   if r(m,n) <= 5

       grid2d(m,n) = 100;

  elseif r(m,n) >= 25

      grid2d(m,n) = 0;

   else

      grid2d(m,n) = A./r(m,n) - B;

   end
    end
end

这给了我一个 2D 内核。现在,如果我将 3D 版本定义为在更大网格内沿 z 轴长的 9 个元素,我可以通过以下方式创建 3D 内核;

gz = 147:155; %9 elements in the z axis
H = length(gz);
kernel3D = zeros(301,301,301); %

for n = 1:H

    kernel3D(151-50:1:151+50,151-50:1:151+50,gz(n)) = grid2d;
end

如果我使用垂直线源,这非常有效,但我很好奇是否可以沿任何所需方向旋转该数组的元素,以便为倾斜源生成内核;例如,假设我想通过 (151,151,151) 处的线源中心将这个阵列相对于 XY 平面旋转 45 度,并相对于 XZ 平面旋转 60 度?

有没有一种优雅的方法可以做到这一点,也许使用旋转矩阵?

【问题讨论】:

    标签: arrays matlab multidimensional-array 3d rotation


    【解决方案1】:

    我想我找到了一个可行但不优雅的解决方案——在这个版本中,我生成了 2D 内核,并将其嵌套在 3D 数组中。然后我使用旋转操作符将某个位置的适当值映射到旋转变换。这很丑陋,但似乎有效。

      %generate a test 2d kernel as before...  
    
    A = 625; B =  25; c = 51;  cen = 151; grid2d = zeros(101,101); 
    
    for m = 1:101
    for n = 1:101
    
    
            r(m,n) = sqrt((m - c).^2 + (n - c).^2 ); %distance of point on grid to centre
    
    %Populating the grid as a kernal 
    
     if r(m,n) <= 5
    
        grid2d(m,n) = 100;
    
    elseif r(m,n) >= 25
    
       grid2d(m,n) = 0;
    
     else
    
       grid2d(m,n) = A./r(m,n) - B;
    
      end
       end
      end
    
    %make a bigger array for this to nest in
    flat3d = zeros(301,301,301);
    range = 101:201; %centre range of new grid
    
    flat3d(range,range,cen) = grid2d;  % inserts grid2d at centre of new flat3d grid!  
    
    %now the incline angles
    theta = input('Enter incline in XZ (vertical) plane in degrees : ' );
    phi = input('Enter incline in XY (horizontal) plane in degrees : ' );
    
    %Now a loop which inclines each point relative to theta around (x,y,z) = (cen,cen,cen);
    
    g = range(1); 
    
    inc3d = zeros(301,301,301); %new inclined grid initialised 
    
    for m = 1:101
    for n = 1:101
    
    
       %the g - cen value is added to the index to map arrays sync up correctly and 
       %keep loop reasonable / avoid overlap outside index ! 
    
      xpart(m,n) = cen + cosd(theta).*(g + m -cen) - sind(theta).*(g + n - cen);  
      xn(m,n) = cen + cosd(phi).*(xpart(m,n) - cen) - sind(phi).*(g + n - cen); %this maps a value of x to new position theta and phi
      yn(m,n) = cen + sind(phi).*(xn(m,n) - cen) + cosd(phi).*(g + n - cen); %maps a value of y to new position relative to phi
      zn(m,n) = cen + sind(theta).*(g + m - cen) + cosd(theta).*(g + n - cen); %maps z to new position relative to theta (doesnt change relative to phi)
    
        xn = round(xn);
        yn = round(yn);  %round to avoid integer problems with indexing 
        zn = round(zn);
    
    
       inc3d(xn(m,n),yn(m,n),zn(m,n)) = flat3d(g + m,g + n,cen);
    
     end
    end
    

    这似乎有效;例如,如果我生成 theta = 30 和 phi = 15 的 plot3 图片,我会得到这样的结果;

    从这张 2D 平面图像中,通过直线方程,我认为可以简单地总结并生成需要任何区域的内核。这很混乱,但似乎有效。如果有人知道更清洁的方法,请务必添加它:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 2010-12-31
      相关资源
      最近更新 更多