MATLAB: Rotating a meshgrid in 3D

3dmeshgrid

Ive got a meshgrid in 2d that i've been rotating so far. I want toe expand the mesh to 3d, and am having some trouble rotating it. This is how i've been doing it for 2D:
xLin = linspace(-10*scale/4,10*scale/4,200);
yLin = linspace(-10*scale/4,10*scale/4,200);
[x,y] = meshgrid(xLin,yLin); % create mesh
angle1 = -1*atan2(k1(1,2),k1(1,1));
XY1 = [x(:) y(:)];
R1=[cos(angle1) -sin(angle1); sin(angle1) cos(angle1)];
rotXY1=XY1*R1';
Xr1 = reshape(rotXY1(:,1), size(x,1), []);
Yr1 = reshape(rotXY1(:,2), size(y,1), []);

Best Answer

The 3d process is much the same, except that you build xyz columns and multiply by a 3x3 array.
It can be easier to use makeghtform to build an appropriate 4x4 matrix. To use the 4x4 matrix, pad the xyz with a column of ones before doing the multiplication. Using the 4x4 permits easier rotation around an arbitrary center.
t=makehgtform(appropriate stuff)
xyz1 = xyz; xyz1(:, 4)=1;
newxyz = (t*xyz1')';