[Math] How to calculate the angle between two vectors in high-dimensional space

geometry

I want to calculate the angle (or the slope) of the line that connects two points $a,b \in R^N$. By high, I mean $N>3$.

How can I do this?

Also, can I describe this line with only an angle, or do i need a bigger data structure like a Quaternion or something? I want the direction of the line, the magnitude is not important.

And I'm trying to do this in MATLAB, so I would really appreciate a code example.

Thanks for any help!

Best Answer

If you want to calculate the angle between two vectors (say, $u$ and $v$) in high-dimensional space, the formula is $$ A = \cos^{-1}\left(\frac{u \cdot v}{\|u\|\|v\|}\right) $$ Where $u\cdot v$ denotes the dot product. In MATLAB:

A = acos((u'*v)/(norm(u)*norm(v)))

So, to get the angles of the projections on the the first $N-1$ axes, let's say, you could do something like this:

v = b-a;

for i = 1:N-1        

    angles(i) = acos(v(i)/norm(v));

end

Or, if you want to skip the for loop,

angles = acos(v/norm(v));