簡體   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