Matrix multiplication between an operation matrix, such as the rotation matrix, and a coordinate list.

Hi
I would like to apply a 3D rotation operation, by matrix multiplication, on each of a element (rows) of coordinate list represented by a N x 3 vector.
For now I use a for-loop over all coordinates, see below, but I would prefer to do this in one operation.
BR
Thomas
y_rotation_matrix=@(theta) [cos(-theta) 0 sin(-theta);0 1 0;-sin(-theta) 0 cos(-theta)];
coordinate_list=rand(10,3)
hold on
xlabel('x')
ylabel('y')
zlabel('z')
plot3(coordinate_list(:,1),coordinate_list(:,2),coordinate_list(:,3));
rotated_coordinate_list=nan(size(coordinate_list));
theta0=.3
for q=1:size(coordinate_list,1)
rotated_coordinate_list(q,:)=y_rotation_matrix(theta0)*coordinate_list(q,:)';
end
plot3(rotated_coordinate_list(:,1),rotated_coordinate_list(:,2),rotated_coordinate_list(:,3));

 Accepted Answer

y_rotation_matrix=@(theta) [cos(-theta) 0 sin(-theta);0 1 0;-sin(-theta) 0 cos(-theta)];
rng(100)
coordinate_list=rand(10,3);
rotated_coordinate_list=nan(size(coordinate_list));
theta0 = .3;
for q=1:size(coordinate_list,1)
rotated_coordinate_list(q,:)=y_rotation_matrix(theta0)*coordinate_list(q,:)';
end
Here is one option
r1 = (y_rotation_matrix(theta0)*coordinate_list.').';
Suprised that it's not exactly the same as result from the loop. Maybe some operations are done in a different order when multiplying two matrices like that?
r1 - rotated_coordinate_list
ans = 10×3
1.0e-15 * 0.0555 0 0 -0.0191 0 0 0 0 0 0 0 0 0 0 0 -0.0026 0 -0.0555 0 0 0 0 0 0 0 0 0.1110 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Here's another option.
r2 = coordinate_list*y_rotation_matrix(theta0).';
Also slightly different than original result
r2 - rotated_coordinate_list
ans = 10×3
1.0e-15 * 0.0555 0 0 -0.0191 0 0 0 0 0 0 0 0 0 0 0 -0.0026 0 -0.0555 0 0 0 0 0 0 0 0 0.1110 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
But am slightly surprised that it's identical to the first method
r2 - r1
ans = 10×3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

1 Comment

Suprised that it's not exactly the same as result from the loop. Maybe some operations are done in a different order when multiplying two matrices like that?
The way matrix operations are multithreaded depends on the size of the operands.

Sign in to comment.

More Answers (0)

Products

Asked:

on 12 Feb 2025

Edited:

on 13 Feb 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!