Vectorized Solution to Rotate Multiple Points each at a Different Angle

8 ビュー (過去 30 日間)
John deGrassie
John deGrassie 2013 年 3 月 7 日
コメント済み: Fvieira 2021 年 4 月 30 日
I want to rotate a matrix of points, i.e. column vectors. However, I want to rotate each vector in the matrix by a different angle. For example:
pointMatrix = [v1,v2,v3,v4]; %vi is a column vector
rotateAngles = [10,20,30,40]; %degrees
Say I want to rotate these points around the z-axis. Therefore, for one point I could do something like the following:
Rz = [[cos(rotateAngles(1)) -sin(rotateAngles(1)) 0];...
[sin(rotateAngles(1)) cos(rotateAngles(1)) 0];...
[ 0 0 1]];
v1Rotated = Rz*v1;
Is there a non-loop way to rotate all the vectors in my pointMatrix by each one's unique rotation angle? Something like this...
allRotatedPoints = superRotationMatrix*pointMatrix;
where the superRotationMatrix "magically" rotates each column by the corresponding angle.
Thanks!

採用された回答

Teja Muppirala
Teja Muppirala 2013 年 3 月 8 日
編集済み: Teja Muppirala 2013 年 3 月 8 日
This vectorized solution uses complex exponentials and works about 2 orders of magnitude faster for large vectors.
M = exp(rotateAngles*1i) .* ([1 1i 0]*pointMatrix);
allRotatedPoints = [real(M); imag(M); pointMatrix(3,:)];
  1 件のコメント
Fvieira
Fvieira 2021 年 4 月 30 日
Notice that this works for 3 coordinate vectors (p = x,y,z).
For 2d one must consider this variation:
M = exp(rotateAngles*1i) .* ([1 1i]*pointMatrix);
allRotatedPoints = [real(M); imag(M)].'
Here is a full example that I did based on Muppirala's answer regarding some small details (angle in radians, for example):
v1 = [1;1]; v2 = [2;2]; v3 = [3;0]; v4 = [4;2];
pointMatrix = [v1 v2 v3 v4] %vi is a column vector
rotateAngles = [90 90 90 90] *pi/180 % radians
M = exp(rotateAngles*1i) .* ([1 1i]*pointMatrix);
allRotatedPoints = [real(M); imag(M)].'
pointMatrix = pointMatrix.'
figure(1)
plot(pointMatrix(:,1),pointMatrix(:,2),'o')
axis([-5 5 -5 5])
grid on, hold on
plot(allRotatedPoints(:,1),allRotatedPoints(:,2),'x')
Here are the plots: Circles are the points, crosses are them rotated by 90 degrees.

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2013 年 3 月 7 日
編集済み: Matt J 2013 年 3 月 7 日
You can use MTIMESX on the file exchange
If your superRotationMatrix is 3x3xN and you reshape your pointMatrix to be 3x1xN, then
mtimesx(superRotationMatrix,pointMatrix)
will give you the rotated vectors in a 3x1xN output.

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by