MATLAB Answers

How to get the desired quaternion representation from a rotation matrix?

6 views (last 30 days)
David Li
David Li on 24 Jun 2021
Commented: David Li on 28 Jun 2021
(For a little bit of context, I'm working with an attitude-determination algorithm called TRIAD and the software called STK, and I'm trying to make their results consistent.)
Let's say I have this rotation matrix:
BN = [0.7505 0.1707 -0.6384
0.1574 0.8921 0.4236
0.6418 -0.4184 0.6427];
(It could be verified that it has a determinant of 1.)
If I convert it to quaternions with the first component being the scalar,
quat = rotm2quat(BN)
the result is [0.9063 0.2323 0.3532 0.0037].
However, my desired representation (from the STK output) is [-0.9063 -0.2323 -0.3532 -0.0037].
It could be verified that these two representations yield the same Euler angles using the "quat2eul" function.
What can I do to achieve the desired quaternion representation? The issue isn't as simple as adding a negative sign, because for the rotation matrix,
rotm = [0.8138 0.4698 -0.3420
-0.4410 0.8826 0.1632
0.3785 0.0180 0.9254];
the converted quaternion representation is exactly the same as the desired quaternion representation.
Thanks so much in advance!
Here's a potentially useful reference from STK:

Accepted Answer

James Tursa
James Tursa on 28 Jun 2021
As you already know, both q and -q represent the same rotation. Which sign to pick is entirely up to the underlying algorithm. So the only way to match STK exactly in all cases is to know exactly how STK picks these signs in all cases. Unless you have access to their underlying algorithms, how do you expect to do this? Has STK published this algorithm for you to use? Short of that, you would probably have to run a bunch of constructed test cases to try and figure it out. What is the reason you must match STK?
The sign choice can affect downstream calculations, so it is important to be consistent. E.g., if this is being passed off to a control system you might want to ensure the quaternion scalar element is non-negative so that the physical rotation the control system commands is the "short" way in instead of the "long" way. Or if you are generating a sequence of quaternions from a sequence of rotation matrices the choice may be to ensure that the largest magnitude quaternion element does not change sign from one q to the next q. Etc.
Bottom line is you will need to figure out what type of consistency makes sense for your application and work with that.
  1 Comment
David Li
David Li on 28 Jun 2021
Thank you. At the time I posted my question, I didn't know enough about quaternions to be confident that every rotation could be represented by two quaternion representations. I later learned about the "short rotations" and "long rotations" corresponding to the two representations, and only then did it dawn on me that the representations described the exact same rotation.
Another reason I was trying to line up the MATLAB representations with STK representations was that I heard quaternions were more numerically stable than Euler angles and so I falsely assumed that if the Euler angles matched up, the quaternions should, too.
The script involving this issue is mostly for demonstration purposes - it serves as a reference (for any member on my MiTEE team) for the implementation of the TRIAD algorithm and intends to show its agreement with the STK data. Now that I know the TRIAD algorithm does agree with STK but there's no easy way to make the calculated quaternion representation identical to that of STK, it makes the most sense to me to discard the quaternion conversions and just stick to Euler angles instead (for clarity purposes). We'd also use the TRIAD algorithm to do attitude determination with the actual data, but the quaternions can also be avoided to do the required calculations.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by