Computing Average Quaternions

2019-03-01 16:54

This is a quick post. Quaternions are a commonly used number system that find use in a range of different applications like computer vision and animation. I’ve mostly used them to calculate three dimensional rotations in some of my aerospace or dynamics classes, or currently, on a research project.

In some cases you might find yourself needing to average quaternions, which is an interesting mathematical problem in and of itself. For more info on the math, check out this paper.

Anyway, I looked online and there’s a disparity between the number of answered and unanswered questions regarding averaging quaternions, so I figured I’d put up my solution (in Matlab) to help anyone out.

function [Qavg] = avgQuaternion(Q)
weights = ones(1, length(Q)); % replace with own weights 

A = (weights.*Q)./sum(weights); 
M = A*transpose(Q); 

[Qavg, ~] = eigs(M, 1); %return largest eigenvector


The average quaternion is the maximum eigenvector you get solving the maximization procedure: $$ \bar{\mathbf{q}} = \text{argmax}\mathbf{q}^{T}M\mathbf{q} $$ where:

$$ M = \sum_{i=1}^{n} w_{i}\mathbf{q}_{i}\mathbf{q}_{i}^{T} $$

and w, q are the weights and quaternions respectively.