繁体   English   中英

如何通过围绕其中心列或中心行旋转2D矩阵在MATLAB中创建3D矩阵?

[英]How to create a 3D matrix in MATLAB by rotating 2D matrix around its center column or center row?

我有一个2D MATLAB矩阵,它相对于其中心列是对称的。 我想绕着其中心列旋转此矩阵,以产生一个3D矩阵,该对象代表具有圆柱对称性的对象。

我想对不同的矩阵执行相同的操作,该矩阵相对于其中心行是对称的。 (这次,我想绕其中心行旋转它以生成3D矩阵)。

我想到的是将链接中给出的想法推广到3D:

如何通过围绕中心元素旋转一维数字矢量来创建2D图像?

但是对MATLAB不够了解对我来说不是一个直截了当的任务。

有人可以帮忙吗?

我刚刚修改了3D的可接受答案

% generate symetric matrix
A = zeros(11,31);
A(4:8,10:22) = repmat([1:7 6:-1:1],[5 1]);
% symmetric x axis
n = floor(size(A,2)/2);

% A vector of distance (measured in pixels) from the center of vector V to each element of V
[r,y] = meshgrid([n:-1:0, 1:n],1:size(A,1));

% Now find the distance of each element of a square 2D matrix from it's centre. @(x,y)(sqrt(x.^2+y.^2)) is just the Euclidean distance function.
ri = sqrt( bsxfun( @(x,y)x.^2+y.^2,r,permute(r,[1 3 2]) ) );
yi = repmat(y,[1 1 size(A,2)]);

% Now use those distance matrices to interpole V
obj = interp2(r(:,1:n+1),y(:,1:n+1),A(:,1:n+1),ri,yi,'nearest');
obj(isnan(obj)) = 0;

% show
[xg,yg,zg] = meshgrid(1:size(obj,2),1:size(obj,1),1:size(obj,3));
scatter3(xg(:),yg(:),zg(:),10,obj(:)+1,'filled')
axis equal

更新 -如果您不想使用interp2 ,则可以执行以下操作:

obj = interp1(r(1,1:n+1).',A(:,1:n+1).',ri(1,:,:),'nearest');
obj = permute(obj,[4,3,2,1]);
obj(isnan(obj)) = 0;

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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