Search code examples
matlabquaternions

How to calculate the norm of quternion in Matlab?


How can I calculate the norm of quaternion in matlab?

I tried this example

a = [1 4 4 -4];
norm = quatnorm(a)

My expected output is 7 but matlab returns 49.

What am I doing wrong?


Solution

  • As @Dan points out, using the native implementation, you are probably getting the square of the formal norm definition. For some reason quatnorm returns the square, after estimating the Euclidean norm (square root of sum of squares).

    q = [1 4 4 -4];
    

    MATLABquatnorm:

    for index = size(q, 1):-1:1
        qnorm(index,:) = norm(q(index,:), 2);
    end    
    qout = qnorm.*qnorm;
    

    Alternative (for vectors):

    sqrt(q*q')
    

    This is equivalent to getting sqrt(quatnorm(q)). As you will note above, quatnorm is also adapted to estimate norms for quaternions stored in successive matrix rows (estimates the norm of each row and then squares)

    Alternative (for matrices N x 4):

    Q = [q; 2*q]; % example
    
    sqrt(diag(Q*Q'))