# Rotating one 3d-vector to another only by using rotations about the coordinate axes.

If I have a vector v=(x,y,z) and would like to transform another vector u by using only rotations about the coordinate axes to be in the direction of v, how can I find required angles and the order of applying the rotations to align u with v. I my case u is initially directed along one of the basis vectors.

For example if v=(1,1,1) and u=(1,0,0) I can find the angle between the v and z axis to be 54.7 Deg and the angle that v projection onto xy plane makes with x axis is 45 Deg. Now using the rotation matrices to rotate u first about y axis and then about z axis aligns the u vector with v.

uv = rotz(45)*roty(54.7)*u'

uv will be aligned with v ( rotz(45) and roty(54.7) are corresponding rotation matrices about y and z axis) The above procedure seems rather cumbersome and not general, also if I switch rotation sequence it will not work.

Answers with octave/matlab example/implementations are welcome.

Here is what I have so far in octave

% initial vector
ux = [1 0 0];
%uy = [0 1 0]';
%uz = [0 0 1]';

% for example, vector to align to
v = [1 1 1];

% find a/b/c angles for rotation matrices
% and sequence of operations
...

rotx = rotv([1,0,0],a*pi()/180);
roty = rotv([0,1,0],b*pi()/180);
rotz = rotv([0,0,1],c*pi()/180);

% rotate the initial vector to be aligned with v
rotmat = % rotx*roty*rotz rotation sequence ?
uv=rotmat*ux';

% check the alignment
th = acosd(dot(v,uv)/(norm(v)*norm(uv)))

• en.wikipedia.org/wiki/Rotation_matrix – user238841 Apr 25 '16 at 17:14
• Find some arbitrary rotation axis and angle that transforms u vector to v and then compute the rotation matrix from the angle and axis and then decompose this matrix in 3 endomorphisms, as indicated in the picture in the section Conversion from and to axis-angle ? Is that the way ? – miquo Apr 26 '16 at 21:27
• Yep, pretty much. Be careful the order checks out; rotations in higher dimensions are inherently non-commutative. – user238841 Apr 27 '16 at 7:35
• Will give a more detailed response later, sorry :( – user238841 Apr 27 '16 at 13:50
• I suppose general algorithm could go like this: 1.) Find u->v rotation axis from the vector cross product. 2.) Find u->v rotation angle from the vector dot product. 3.) From rotation axis and angle find the rotation matrix R using Rodrigues formula. 4.) Decompose the rotation matrix R in terms of Euler angles. 5.) From Euler angles compose the elementary rotation matrices Rx Ry Rz. R is given by R = RzRyRz, then u->v rotation in terms of elementary rotations about X/Y/Z axis is given by v=Ru=Rz*(Ry*(Rzu)) ? – miquo Apr 29 '16 at 13:32