MATLAB Answers

How can I determine the angle between two vectors in MATLAB?

2,076 views (last 30 days)
How can I determine the angle between two vectors in MATLAB?
I have two vectors. Is there a MATLAB function that can determine the angle between them?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Feb 2016
There is no MATLAB function that can determine the angle between two vectors.
As a workaround, you can find the norm of the cross product using the CROSS function and the dot product using the DOT function and then find the four quadrant inverse tangent in degrees using the 'atan2d' function.
For example:
u = [1 2 0];
v = [1 0 0];
ThetaInDegrees = atan2d(norm(cross(u,v)),dot(u,v));
You can also divide the dot product of the two vectors obtained using the DOT function by the product of magnitudes of the two vectors (NORM function), to get the cosine of the angle between the two vectors. This does not work well for small angles.
For example:
 
u = [1 2 0];
v = [1 0 0];
CosTheta = dot(u,v)/(norm(u)*norm(v));
ThetaInDegrees = acosd(CosTheta);
For more information on the functions DOT, CROSS, , ACOSD, ATAN2D and NORM, refer the following URLs:
DOT:
CROSS:
ACOSD:
ATAN2D
NORM:

  7 Comments

Show 4 older comments
Ivo Maljevic
Ivo Maljevic on 19 Nov 2018
It would be good to have negative angle when the second vector is to the "right" of the first vector. A minor twek to the code above would work in 2D case (vectors a and b have 2 elements each):
a_3d = [a,0];
b_3d = [b, 0];
c = cross(a_3d,b_3d);
theta = sign(c(3))*180/pi*atan2(norm(c),dot(a,b)); % calculate degrees
Still does not work for [1,0] and [-1, 0], but works for [1,0] and [1,-1] - the output is -45 as opposed to [1, 0] and [1, 1], where the second vector is to the "left" of the first, so the angle is 45. Swapping out the order of the same two vectors also works: [1, 1] with [1, 0] produces -45.
Seamus MacInnes
Seamus MacInnes on 28 May 2019
I liked @Ivo Maljevic's answer and would like to add that to fix the [1,0] and [-1,0] problem you can use a small if statement. The problem happens because the cross product of parallel lines is 0 and the sign() function returns a 0 when its argument is 0. Here is the workaround:
Radians:
if c(3) < 0
theta = -atan2(norm(c),dot(a,b));
else
theta = atan2(norm(c),dot(a,b));
end
For degrees, replace atan2 with atan2d
Johannes Kalliauer
Johannes Kalliauer on 14 Jan 2020 at 14:55
u = [1 2 0];
v = [1 0 0];
CosTheta = dot(u,v)/(norm(u)*norm(v));
ThetaInDegrees = acosd(CosTheta);
Almost linear Vectors (Angle of say 8E-10rad) might leed to problems since CosTheta might be slightly bigger than 1, due to nummerical double precission (say something around 1+2E-16) and leed to imaginary angles
u = [1 2 0];
v = [1 0 0];
CosTheta = max(min(dot(u,v)/(norm(u)*norm(v)),1,-1);
ThetaInDegrees = acosd(CosTheta);

Sign in to comment.

More Answers (7)

Pierre-Pascal
Pierre-Pascal on 11 Jan 2016
So why doesn't matlab give us a function for that instead of having us look endlessly on forums?

  1 Comment

William Chamberlain
William Chamberlain on 27 Jul 2016
Agreed: it is a very standard function to perform, and a strange omission from Matlab's function set.

Sign in to comment.


James Tursa
James Tursa on 9 Jul 2015
Edited: James Tursa on 5 Jan 2019
This topic has been discussed many times on the Newsgroup forum ... if I looked hard enough I'm sure I could find several Roger Stafford posts from many years ago on this. E.g., here is one of them:
The basic acos formula is known to be inaccurate for small angles. A more robust method is to use both the sin and cos of the angle via the cross and dot functions. E.g.,
atan2(norm(cross(u,v)),dot(u,v));
An extreme case to clearly show the difference:
>> a = 1e-10 % start with a very small angle
a =
1e-10
>> u = 4*[1 0 0] % arbitrary non-unit vector in X direction
u =
4 0 0
>> v = 5*[cos(a) sin(a) 0] % vector different from u by small angle
v =
5 5e-10 0
>> acos(dot(u,v)/(norm(u)*norm(v))) % acos formulation does not recover the small angle
ans =
0
>> atan2(norm(cross(u,v)),dot(u,v)) % atan2 formulation does recover the small angle
ans =
1e-10

  3 Comments

meenakshi
meenakshi on 4 Jan 2019
thank u, i used in color vector in ycbcr space. It is very iseful to me
Johannes Kalliauer
Johannes Kalliauer on 14 Jan 2020 at 15:05
Thanks, sometimes even imaginäry Angles occur if using acos-function.

Sign in to comment.


Gabor Bekes
Gabor Bekes on 15 Sep 2016
Edited: Gabor Bekes on 15 Sep 2016
This does the same thing, also capable of determining the angle of higher (than one) dimensional subspaces.
subspace(vector1,vector2)

  1 Comment

Aras
Aras on 3 May 2018
This method needs to be used carefully because it provides an angle between 0 and π/2 radians, instead of between 0 and π.
E.g., the angle between vectors [1, 0] and [-1, 0] are given as 0, while the result is expected to be π, considering their opposite directions.

Sign in to comment.


Daniel Vasilaky
Daniel Vasilaky on 9 Jul 2015
Edited: Walter Roberson on 15 Sep 2015
acosd(CosTheta)
will give you the same answer.

  0 Comments

Sign in to comment.


Boris Povazay
Boris Povazay on 17 Jun 2018
Just a note on how to vectorize the whole thing: (semicolons purposely omitted to see the intermediate results)
u = [1 2 0];
v = [1 0 0];
C=cross(u,v)
NC=norm(C)
D=dot(u,v)
ThetaInDegrees = atan2d(NC,D)
Rep=5
uf = repmat(u,5,1)
vf = repmat(v,5,1)
vC=cross(uf,vf,2) %vectorized
vNC=vecnorm(vC,2,2) % since only z-rotation is allowed anyway, this is equivalent to: vNC=vC(:,3)
vD=dot(uf,vf,2)
vThetaInDegrees = mean(atan2d(vNC,vD))
or in short (the hard to read variant)
VThetaInDegrees =atan2d( vecnorm(cross(Vu,Vv,2),2,2) , dot(Vu,Vv,2) )

  2 Comments

Boris Povazay
Boris Povazay on 17 Jun 2018
One more thing to mention: this calculation takes the norm and therefore is not the solution to the question: angle between vectors! - It is rather the angle between unoriented vectors. The solution to the question rather should result in [-180:+180] to distinguish the orientation of the angle. - It is mentioned here.
-> So how can this be rewritten without loosing the orientation due to the norm?
Jan
Jan on 17 Jun 2018
@Boris Povazay: I do not agree. The range of [-180, 180] is meaningful in the 2D case only. In 3D (and higher dimensions) the sign of the angle cannot be defined, because it would depend on the direction of view. You need a third vector to define the direction of view to get the information about the sign. Therefore the answer is correct: In the general case the angle between two vectors is the included angle: 0 <= angle <= 180.

Sign in to comment.


theodore panagos
theodore panagos on 29 Oct 2018
Coordinates of two vectors xb,yb and xa,ya .
angle(vector.b,vector.a)=pi/2*((1+sgn(xa))*(1-sgn(ya^2))-(1+sgn(xb))*(1-sgn(yb^2)))
+pi/4*((2+sgn(xa))*sgn(ya)-(2+sgn(xb))*sgn(yb))
+sgn(xa*ya)*atan((abs(xa)-abs(ya))/(abs(xa)+abs(ya)))
-sgn(xb*yb)*atan((abs(xb)-abs(yb))/(abs(xb)+abs(yb)))

  0 Comments

Sign in to comment.


Adana Mason
Adana Mason on 28 Nov 2019
(a) Find parametric equations for the line of intersection of the planes and (b) find the angle
between the planes. 3𝑥 − 2𝑦 + 𝑧 = 1, 2𝑥 + 𝑦 − 3𝑧 = 3.

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R13SP1