0
votes

I have a 2D MATLAB matrix, which is symmetric with respect to its center column. I want to rotate this matrix around its center column to produce a 3D matrix representing an object with a cylindrical symmetry.

The same thing I want to do with a different matrix, which is symmetric with respect to its center row. (This time I want to rotate it around its center row to produce the 3D matrix).

What I had in mind is to generalize to 3D the idea given in the link:

How to create a 2D image by rotating 1D vector of numbers around its center element?

But not knowing MATLAB well enough it is not a so straight forward task for me.

Can someone help please?

1

1 Answers

0
votes

I just modified the accepted answer to 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

UPDATE - if you don't want to use interp2 you can do:

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;

enter image description here