ankit agrawal
ankit agrawal

Reputation: 311

MATLAB and python gives different answer for quaternion

I have a rotation matrix in MATLAB

PC=[0.4822    0.5070    0.7145
   -0.4086    0.8516   -0.3285
    0.7750    0.1336   -0.6177]; 

quat=rotm2quat(PC); %gives me 
[0.3937, 0.8641, 0.0319, 0.3119] %which is [w,x,y,z] 

Same matrix in python

from scipy.spatial.transform import Rotation as R
rot=[[0.4822 ,   0.5070 ,   0.7145],[-0.4086 ,   0.8516 ,  -0.3285],[ 0.7750 ,   0.1336 ,  -0.6177]]
r=R.from_dcm(rot)
print(r.as_quat()) # gives me following   
[ 0.04920064  0.99356301 -0.09745128 -0.0302504 ] # which is [x,y,z,w]

Why the quaternion value [x,y,z,w] are not matching between MATLAB and python.

Upvotes: 0

Views: 714

Answers (1)

James Tursa
James Tursa

Reputation: 2636

PC is not a valid direction cosine (i.e., rotation) matrix. The determinant should be close to 1, but it is actually close to -1. Feeding this PC to any routine expecting a direction cosine matrix will not produce correct results. You need to examine how you are generating this PC matrix and fix it. I can't even reproduce your quaternion. E.g.,

>> PC=[0.4822    0.5070    0.7145
   -0.4086    0.8516   -0.3285
    0.7750    0.1336   -0.6177]; 
>> det(PC)
ans =
   -1.0001   <-- bad
>> quat=rotm2quat(PC)
quat =
    0.9427    0.2430   -0.1641   -0.1590

Upvotes: 1

Related Questions