1,324 views (last 30 days)
Show older comments
MathWorks Support Team on 22 Jun 2011
Commented: Bruno Luong on 3 Dec 2022
Accepted Answer: MathWorks Support Team
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?
Sign in to answer this question.
Accepted Answer
MathWorks Support Team on 27 May 2020
Edited: MathWorks Support Team on 27 May 2020
Open in MATLAB Online
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));
5 Comments Show 3 older commentsHide 3 older comments
Show 3 older commentsHide 3 older comments
Roger Stafford on 9 Jul 2015
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_297320
Edited: James Tursa on 9 Jul 2015
@Mathworks Support Team: I think you will find that Mathworks' 'atan2' function is more accurate than 'acos' for angles that are near zero or pi radians. Just look at a plot of acos(x) to see why.
James Tursa on 24 May 2018
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_571702
@Felix: Did you expect something different?
Johannes Kalliauer on 14 Jan 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_786018
Open in MATLAB Online
@MathWorks Support Team
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);
Johannes Kalliauer on 3 Feb 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_792963
Open in MATLAB Online
@MathWorks Support Team
u=[0.272379472472602111022302462516 1.08301805439555555910790149937 -0.359366773005409555910790149937];
v=[0.2898030626583580555111512312578 1.15229663744866689137553104956 -0.382354774507524222044604925031];
CosTheta = (dot(u,v) / (norm(u)*norm(v)));
if abs(CosTheta)>1
error('MATLAB:odearguments:NumericPrecision','Matlab has numerical issues in calculated angle')
end
leads to an error (imaginär angle), since CosTheta=1+2.22044604925031e-16>1
Solution would be
CosTheta = max(min(dot(u,v)/(norm(u)*norm(v)),1,-1);
ThetaInDegrees = real(acosd(CosTheta));
.
Akihumi on 27 May 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_867093
Open in MATLAB Online
Hi, did you miss out a bracket for the min? I got an error and only resolve it with the following code instead.
CosTheta = max(min(dot(u,v)/(norm(u)*norm(v)),1),-1);
ThetaInDegrees = real(acosd(CosTheta));
Sign in to comment.
More Answers (2)
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?
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
James Tursa on 9 Jul 2015
Edited: James Tursa on 5 Jan 2019
Open in MATLAB Online
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:
https://www.mathworks.com/matlabcentral/answers/161515-how-do-i-find-the-angle-between-a-vector-and-a-line
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 =
>> atan2(norm(cross(u,v)),dot(u,v)) % atan2 formulation does recover the small angle
ans =
1e-10
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Johannes Kalliauer on 14 Jan 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_786021
Thanks, sometimes even imaginäry Angles occur if using acos-function.
James Tursa on 3 Feb 2020
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_793139
To get a full circle result where "direction" of the angle is important, see this link for one possible strategy:
https://www.mathworks.com/matlabcentral/answers/501449-angle-betwen-two-3d-vectors-in-the-range-0-360-degree
Bruno Luong on 3 Dec 2022
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab#comment_2498942
@Felix Fischer If you want to find angles of multiple vector pairs put in matrix, use vecnorm rather than norm.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLAB
Find more on MATLAB in Help Center and File Exchange
Tags
- angle
- vectors
- dot
- theta
Products
- MATLAB
Release
R13SP1
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office