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

2,387 views (last 30 days)
MathWorks Support Team on 22 Jun 2011
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?

MathWorks Support Team on 27 May 2020
Edited: MathWorks Support Team on 27 May 2020
There is no in-built MATLAB function to find the angle between two vectors. As a workaround, you can try the following:
CosTheta = max(min(dot(u,v)/(norm(u)*norm(v)),1),-1);
ThetaInDegrees = real(acosd(CosTheta));
Darko Kulic on 6 Nov 2022
Have a look at my updated answer, you can calculate the angle between two subspaces:
u = [1,1,1]'
v = [2,2,1]'
angle= subspace(u, v)
The Subspace Function was added 2006a.

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?
##### 2 CommentsShowHide 1 older comment
Felix Fischer on 16 Nov 2022
I've wasted many hours fighting with atan2d and cosd to give me the same results, for easy vectors... but it just doenst work.

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
>> 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
Fryderyk Kukowski on 4 Dec 2022
Oh, ok. Thanks!

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)
Aras on 3 May 2018
Edited: 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.

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

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 CommentsShowHide 1 older comment
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.

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)))

Mahaveer Singh on 2 May 2021
Edited: Mahaveer Singh on 4 May 2021
function angle_in_degrees = vector2angle(u,v)
a= sqrt(u(1)^2+u(2)^2+u(3)^2);
b=sqrt(v(1)^2+v(2)^2+v(3)^2);
c=0;
for i=1:1:numel(u)
c=c+u(i)*v(i);
end
angle_in_degrees=acos(c/(a*b))*180/pi
end
Mahaveer Singh on 4 May 2021
%other one is
function angle_in_degrees = vector2angle(u,v)
a= sqrt(u(1)^2+u(2)^2+u(3)^2);
b=sqrt(v(1)^2+v(2)^2+v(3)^2);
angle_in_degrees=acos(dot(u,v)/(a*b))*180/pi
end

Dhritishman on 3 Jul 2022
Currently, there is no built-in MATLAB function to calculate the angle between two vectors. However, you can use dot product property of two vectors to find the angle:
cosOfAngle = max(min(dot(u,v)/(norm(u)*norm(v)),1),-1);
angleInDegrees = real(acosd(cosOfAngle));
DGM on 4 Jul 2022
This is essentially a duplicate of the main answer with the variable names slightly changed.

Darko Kulic on 6 Nov 2022
Edited: Darko Kulic on 6 Nov 2022
Hi, i would like to update the answer:
u = [1,1,1]'
v = [2,2,1]'
angle= subspace(u, v)
The subspace Function was added 2006a.
Jan on 16 Nov 2022

### Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

R13SP1

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!