Angle between two matlab vectors

I want to calculate the angle between two vectors V = [Vx Vy Vz] and B = [Bx By Bz] . Is this formula correct?

 VdotB = (Vx*Bx + Vy*By + Vz*Bz) Angle = acosd (VdotB / norm(V)*norm(B)) 

and is there any other way to calculate it?

My question is not to normalize vectors or make it easier. I ask how to get the angle between these two vectors

+4
source share
4 answers

Based on this link , this is the most stable solution:

 atan2(norm(cross(a,b)), dot(a,b)) 
+14
source

There are many options:

 a1 = atan2(norm(cross(v1,v2)), dot(v1,v2)) a2 = acos(dot(v1, v2) / (norm(v1) * norm(v2))) a3 = acos(dot(v1 / norm(v1), v2 / norm(v2))) a4 = subspace(v1,v2) 

All formulas from this mathworks stream . They say that a3 is the most stable, but I do not know why.

For several vectors stored in the columns of the matrix, you can calculate the angles using this code:

 % Calculate the angle between V (d,N) and v1 (d,1) % d = dimensions. N = number of vectors % atan2(norm(cross(V,v2)), dot(V,v2)) c = bsxfun(@cross,V,v2); d = sum(bsxfun(@times,V,v2),1);%dot angles = atan2(sqrt(sum(c.^2,1)),d)*180/pi; 
+1
source

You can calculate VdotB much faster for vectors of arbitrary length using the point operator, namely:

 VdotB = sum(V(:).*B(:)); 

In addition, as mentioned in the comments, Matlab has a dot function for directly computing internal products.

In addition, a formula is what it does, so you do it right.

0
source

This function should return an angle in radians.

 function [ alpharad ] = anglevec( veca, vecb ) % Calculate angle between two vectors alpharad = acos(dot(veca, vecb) / sqrt( dot(veca, veca) * dot(vecb, vecb))); end anglevec([1 1 0],[0 1 0])/(2 * pi/360) >> 45.00 
0
source

Source: https://habr.com/ru/post/1497947/


All Articles