Why do matlab and python convert Euler angles to quaternions with different results?

18 views (last 30 days)
I converted the following Euler angles/radians into quaternions using matlab's built-in function eul2quat, the external order is: first around the x-axis, then around the y-axis, and finally around the z-axis.
eulars = [-1.78E-15 -0.09548759 -3.141592;
-1.78E-15 -0.09548759 -3.141592;
0 -0.1113647 -3.136936;
-0.000419181 -0.1113141 -3.132684;
-0.000802144 -0.1113117 -3.128802;
-0.001152084 -0.1113079 -3.12526;
-0.001180313 -0.1101197 -3.122032;
-0.001413606 -0.1086281 -3.119092;
-0.001928661 -0.1062565 -3.116415];
matlab_q = eul2quat(eulars,"ZYX")% first x, then y,final z
matlab_q = 9×4
0.0000 -0.9989 -0.0000 -0.0477 0.0000 -0.9989 -0.0000 -0.0477 0.0023 -0.9984 -0.0001 -0.0557 0.0044 -0.9984 -0.0000 -0.0556 0.0064 -0.9984 0.0000 -0.0556 0.0081 -0.9984 0.0001 -0.0556 0.0097 -0.9984 0.0001 -0.0550 0.0112 -0.9985 0.0001 -0.0543 0.0125 -0.9985 0.0003 -0.0531
Then I also used python to convert the above data to quaternions as follows:
from scipy.spatial.transform import Rotation as R
import numpy as np
myeulars = np.array([[-1.78E-15, -0.09548759, -3.141592],
[-1.784486e-15, -0.09548759, -3.141592],
[0, -0.1113647 , -3.136936],
[-0.000419181 , -0.1113141, -3.132684],
[-0.000802144, -0.1113117, -3.128802],
[-0.001152084 , -0.1113079, -3.12526],
[-0.001180313 , -0.1101197, -3.122032],
[-0.001413606 , -0.1086281, -3.119092],
[-0.001928661 , -0.1062565, -3.116415]])
python_q = R.from_euler('xyz', myeulars, degrees=False).as_quat() # first x, then y,final z
Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.
print(python_q)
[[-4.77256586e-02 -1.55965008e-08 -9.98860482e-01 3.26422508e-07]
[-4.77256586e-02 -1.55965008e-08 -9.98860482e-01 3.26422508e-07]
[-5.56534295e-02 -1.29579605e-04 -9.98447432e-01 2.32471611e-03]
[-5.56286986e-02 -3.85220084e-05 -9.98441671e-01 4.43575562e-03]
[-5.56285404e-02 4.46916188e-05 -9.98431257e-01 6.36307029e-03]
[-5.56280572e-02 1.20882279e-04 -9.98418522e-01 8.12154965e-03]
[-5.50351559e-02 5.10113472e-05 -9.98436979e-01 9.73287224e-03]
[-5.42918402e-02 9.49786087e-05 -9.98462346e-01 1.11951287e-02]
[-5.31111500e-02 2.94403626e-04 -9.98510080e-01 1.25195214e-02]]
The above data result is left-right symmetric, according to the documentation, matlab uses quaternions of the form w+x*i+y*j+z*k output; while python uses quaternions of the form x*i+y*j+z*k+w.
It can be inferred that the matlab calculation result (a,b,c,d) should correspond to the python order (w,x,y,z), why the above result corresponds to the python is (w,z,y,x)?
b = quaternion(eulars,'euler','XYZ','point') % However, this result is consistent with Python
9×1 quaternion array
3.2642e-07 - 0.047726i - 1.5597e-08j - 0.99886k
3.2642e-07 - 0.047726i - 1.5597e-08j - 0.99886k
0.0023247 - 0.055653i - 0.00012958j - 0.99845k
0.0044358 - 0.055629i - 3.8522e-05j - 0.99844k
0.0063631 - 0.055629i + 4.4692e-05j - 0.99843k
0.0081215 - 0.055628i + 0.00012088j - 0.99842k
0.0097329 - 0.055035i + 5.1011e-05j - 0.99844k
0.011195 - 0.054292i + 9.4979e-05j - 0.99846k
0.01252 - 0.053111i + 0.0002944j - 0.99851k

Accepted Answer

Brian Fanous
Brian Fanous on 2 Mar 2023
@cui I believe you saw my post on other thread already. But in case someone else comes along this question, the way the MATLAB quaternion class handles euler angle conversions is detailed here:

More Answers (0)

Community Treasure Hunt

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

Start Hunting!