rotate a 4d matrix about each axis by an amount specified in radians in a 4-element input array in matlab

To rotate a 4D matrix about each axis by an amount specified in radians in MATLAB, you can use the built-in functions rotate, axang2rotm, and matlab.graphics.axis.quiver as shown below.

main.m
% Input 4D matrix
A = rand([3,3,3,3]);

% Amount of rotation (in radians) about each axis
angles = [pi/2, pi/4, pi/6, pi/8];

% Rotation about x-axis
rot_matrix_x = axang2rotm([1 0 0 angles(1)]);
A = rotate(A, rot_matrix_x, [1 2 3]);

% Rotation about y-axis
rot_matrix_y = axang2rotm([0 1 0 angles(2)]);
A = rotate(A, rot_matrix_y, [2 3 4]);

% Rotation about z-axis
rot_matrix_z = axang2rotm([0 0 1 angles(3)]);
A = rotate(A, rot_matrix_z, [1 3 4]);

% Rotation about 4th axis
rot_matrix_w = axang2rotm([0 0 0 angles(4)]);
[n1, n2, n3, n4] = size(A);
quiver3(reshape(A(:,:,:,:),n1*n2*n3,n4),zeros(n1*n2*n3),zeros(n1*n2*n3),zeros(n1*n2*n3), 'r');
A = reshape(rotate(reshape(A(:,:,:,:),n1*n2*n3,n4), rot_matrix_w), n1, n2, n3, n4);
quiver3(reshape(A(:,:,:,:),n1*n2*n3,n4),zeros(n1*n2*n3),zeros(n1*n2*n3),zeros(n1*n2*n3), 'b');
827 chars
25 lines

The code above first generates a random 4D matrix A of size 3x3x3x3. The amount of rotation about each axis is specified using the input array angles. Using the axang2rotm function, we generate the rotation matrices for each axis. Then, we apply the rotations using the rotate function for the first three axes and matrix multiplication for the fourth axis. Finally we plot the rotating forth dimension using matlab.graphics.axis.quiver.

gistlibby LogSnag