欧拉角转旋转矩阵matlab代码
时间: 2024-12-08 16:16:54 浏览: 10
在MATLAB中,欧拉角(通常指Roll-Pitch-Yaw三轴旋转)转换为旋转矩阵是一个常见的任务。下面是一个简单的示例代码,它将欧拉角输入(假设为 XYZ 方式,即先绕X轴,然后Y轴,最后Z轴)转换为旋转矩阵:
```Matlab
function rot_mat = euler_to_rot_matrix(euler_angles)
% Unpack the Euler angles (in radians) as [roll, pitch, yaw]
roll = euler_angles(1);
pitch = euler_angles(2);
yaw = euler_angles(3);
% Convert to ZYX order (since MATLAB uses column-major matrices)
% and adjust for the different rotation sequence
Cz = cos(yaw);
Sz = sin(yaw);
Cy = cos(pitch);
Sy = sin(pitch);
Rx = cos(roll);
Sx = sin(roll);
% Construct the rotation matrix
rot_mat = [
Rx * Cy * Cz - Sx * Sy * Sz,
Rx * Sy * Cz + Sx * Cy * Sz,
-Rx * Sz + Sx * Cy * Cz,
-Sx * Cy,
Rx * Cy,
Sy,
Sz * Sx * Cy + Cz * Rx,
-Sz * Sx * Sy + Cz * Rx * Cy,
Cz * Sz
];
end
```
你可以通过传入一个包含三个元素的向量来调用这个函数,比如`rot_mat = euler_to_rot_matrix([0.7, 0.5, 1.2])`。
阅读全文