Convert unit quaternion(s) to rotation matrices.
R = quat2rotmat(q0,q1,q2,q3) R = quat2rotmat(q)
Computes the rotation matrices corresponding to the input unit quaternion(s). The unit quaternion(s), which must be normalized to one, can be given as a 4xN array or using the individual components q0
, q1
, q2
, q3
(each of size 1xN). The corresponding rotation matrices are returned as an array
R
of size 3x3xN.
Note that quat2rotmat
is vectorized and will accept arrays of unit quaternions of size 4xNxMx... with an arbitrary number of dimensions, in which case the rotation matrices will be returned as an array of size 3x3xNxMx... .
For details about rotation matrices and orientations, see the page on relative orientations.
Taking four arbitrary quaternion components and normalizing the result, we compute the rotation matrix
q1 = [0.1; 0.438; -0.879; 0.699]; q1 = q1./sqrt(sum(q1.^2))
q1 = 0.0827 0.3621 -0.7267 0.5779
R = quat2rotmat(q1)
R = -0.7241 -0.6218 0.2984 -0.4307 0.0698 -0.8998 0.5387 -0.7800 -0.3184
Now, feeding this rotation matrix into rotmat2quat
,
we get back the additive inverse of the four original unit quaternion components, -q1.
q2 = rotmat2quat(R)
q2 = -0.0827 -0.3621 0.7267 -0.5779
Note that, for unit quaternions, -q and q represent equivalent rotations.
vec = [1; 1; 1]./sqrt(3); quatvecmult(q1,vec) - quatvecmult(q2,vec)
ans = 0 0 0
rotmat2quat, quat2euler, euler2quat, quatvecmult