rotmat
Convert quaternion to rotation matrix
Description
converts the quaternion, rotationMatrix
= rotmat(quat
,rotationType
)quat
, to an equivalent rotation matrix
representation.
Examples
Convert Quaternion to Rotation Matrix for Point Rotation
Define a quaternion for use in point rotation.
theta = 45; gamma = 30; quat = quaternion([0,theta,gamma],"eulerd","ZYX","point")
quat = quaternion
0.8924 + 0.23912i + 0.36964j + 0.099046k
Convert the quaternion to a rotation matrix.
rotationMatrix = rotmat(quat,"point")
rotationMatrix = 3×3
0.7071 -0.0000 0.7071
0.3536 0.8660 -0.3536
-0.6124 0.5000 0.6124
To verify the rotation matrix, directly create two rotation matrices corresponding to the rotations about the y- and x-axes. Multiply the rotation matrices and compare to the output of rotmat
.
theta = 45; gamma = 30; ry = [cosd(theta) 0 sind(theta) ; ... 0 1 0 ; ... -sind(theta) 0 cosd(theta)]; rx = [1 0 0 ; ... 0 cosd(gamma) -sind(gamma) ; ... 0 sind(gamma) cosd(gamma)]; rotationMatrixVerification = rx*ry
rotationMatrixVerification = 3×3
0.7071 0 0.7071
0.3536 0.8660 -0.3536
-0.6124 0.5000 0.6124
Convert Quaternion to Rotation Matrix for Frame Rotation
Define a quaternion for use in frame rotation.
theta = 45; gamma = 30; quat = quaternion([0,theta,gamma],"eulerd","ZYX","frame")
quat = quaternion
0.8924 + 0.23912i + 0.36964j - 0.099046k
Convert the quaternion to a rotation matrix.
rotationMatrix = rotmat(quat,"frame")
rotationMatrix = 3×3
0.7071 -0.0000 -0.7071
0.3536 0.8660 0.3536
0.6124 -0.5000 0.6124
To verify the rotation matrix, directly create two rotation matrices corresponding to the rotations about the y- and x-axes. Multiply the rotation matrices and compare to the output of rotmat
.
theta = 45; gamma = 30; ry = [cosd(theta) 0 -sind(theta) ; ... 0 1 0 ; ... sind(theta) 0 cosd(theta)]; rx = [1 0 0 ; ... 0 cosd(gamma) sind(gamma) ; ... 0 -sind(gamma) cosd(gamma)]; rotationMatrixVerification = rx*ry
rotationMatrixVerification = 3×3
0.7071 0 -0.7071
0.3536 0.8660 0.3536
0.6124 -0.5000 0.6124
Convert Quaternion Vector to Rotation Matrices
Create a 3-by-1 normalized quaternion vector.
qVec = normalize(quaternion(randn(3,4)));
Convert the quaternion array to rotation matrices. The pages of rotmatArray
correspond to the linear index of qVec
.
rotmatArray = rotmat(qVec,"frame");
Assume qVec
and rotmatArray
correspond to a sequence of rotations. Combine the quaternion rotations into a single representation, then apply the quaternion rotation to arbitrarily initialized Cartesian points.
loc = normalize(randn(1,3)); quat = prod(qVec); rotateframe(quat,loc)
ans = 1×3
0.9524 0.5297 0.9013
Combine the rotation matrices into a single representation, then apply the rotation matrix to the same initial Cartesian points. Verify the quaternion rotation and rotation matrix result in the same orientation.
totalRotMat = eye(3); for i = 1:size(rotmatArray,3) totalRotMat = rotmatArray(:,:,i)*totalRotMat; end totalRotMat*loc'
ans = 3×1
0.9524
0.5297
0.9013
Input Arguments
quat
— Quaternion to convert
quaternion
object | array of quaternion
objects
Quaternion to convert, specified as a quaternion
object or an array of quaternion
objects of any dimensionality.
rotationType
— Type or rotation
"frame"
| "point"
Type of rotation represented by the rotationMatrix
output, specified as "frame"
or
"point"
.
Data Types: char
| string
Output Arguments
rotationMatrix
— Rotation matrix representation
3-by-3 numeric matrix | 3-by-3-by-N numeric array
Rotation matrix representation, returned as a 3-by-3 numeric matrix or 3-by-3-by-N numeric array.
If
quat
is a scalar,rotationMatrix
is returned as a 3-by-3 matrix.If
quat
is non-scalar,rotationMatrix
is returned as a 3-by-3-by-N array, whererotationMatrix(:,:,i)
is the rotation matrix corresponding toquat(i)
.
The data type of the rotation matrix is the same as the underlying data
type of quat
.
Data Types: single
| double
Algorithms
Given a quaternion of the form
the equivalent rotation matrix for frame rotation is defined as
The equivalent rotation matrix for point rotation is the transpose of the frame rotation matrix:
References
[1] Kuipers, Jack B. Quaternions and Rotation Sequences: A Primer with Applications to Orbits, Aerospace, and Virtual Reality. Princeton, NJ: Princeton University Press, 2007.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Version History
Introduced in R2018a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)