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
MathWorks Support Team 2020 年 5 月 27 日
編集済み: MathWorks Support Team 2020 年 5 月 27 日

14 投票

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

7 件のコメント

Roger Stafford
Roger Stafford 2015 年 7 月 9 日
編集済み: James Tursa 2015 年 7 月 9 日
@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.
Pierre-Pascal
Pierre-Pascal 2016 年 1 月 11 日
移動済み: John D'Errico 2024 年 9 月 28 日
So why doesn't matlab give us a function for that instead of having us look endlessly on forums?
James Tursa
James Tursa 2018 年 5 月 24 日
@Felix: Did you expect something different?
Johannes Kalliauer
Johannes Kalliauer 2020 年 1 月 14 日
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
Johannes Kalliauer 2020 年 2 月 3 日
@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
Akihumi 2020 年 5 月 27 日
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));
Bruno Luong
Bruno Luong 2024 年 9 月 28 日
編集済み: Bruno Luong 2024 年 9 月 28 日
This is actually incorrect for complex vectors
CosTheta = max(min(dot(u,v)/(norm(u)*norm(v)),1),-1);
ThetaInDegrees = real(acosd(CosTheta));
The correct code is
CosTheta = max(min(real(dot(u,v))/(norm(u)*norm(v)),1),-1);
ThetaInDegrees = acos(CosTheta)

サインインしてコメントする。

その他の回答 (2 件)

James Tursa
James Tursa 2015 年 7 月 9 日
編集済み: James Tursa 2019 年 1 月 5 日

11 投票

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 件のコメント

Johannes Kalliauer
Johannes Kalliauer 2020 年 1 月 14 日
Thanks, sometimes even imaginäry Angles occur if using acos-function.
James Tursa
James Tursa 2020 年 2 月 3 日
To get a full circle result where "direction" of the angle is important, see this link for one possible strategy:
Bruno Luong
Bruno Luong 2022 年 12 月 3 日
@Felix Fischer If you want to find angles of multiple vector pairs put in matrix, use vecnorm rather than norm.

サインインしてコメントする。

Bruno Luong
Bruno Luong 2024 年 9 月 28 日
編集済み: Bruno Luong 2024 年 9 月 28 日

3 投票

There is a good formula from Kahan, chap 12 of this Mindless paper, for given x and y two vectors of length(m) - in R^m, the angle theta between x and y can be computed as
nx = norm(x);
ny = norm(y);
xx = x*ny;
yy = y*nx;
a = xx-yy;
b = xx+yy;
theta = 2*atan(sqrt(sum(a.^2)/sum(b.^2)))
The advantage of this method is good stability and in case of
nx = norm(x) = ny = norm(y) (= 1, not required)
the code can be reduced to
a = x-y;
b = x+y;
theta = 2*atan(sqrt(sum(a.^2)/sum(b.^2)))
or more compactly
theta = 2*atan(sqrt(sum((x-y).^2)/sum((x+y).^2)))
% or
theta = 2*atan(norm(x-y)/norm(x+y))
The number of arithmetic operations is less than the atan2 formula in James Tursa's answer (only applied in R^3) which is numericall more stable than TMW answer using only dot product.
Note that this implementation does not have issue when b is all-0 vector. But in case both x and y are 0s - so as a and b, Kahan method returns NaN rather than 0 as with atan2. IMO NaN is mathemetically more coherent result.
Beside this degenerated case the result is in interval [0,pi].
NOTE: For complex vectors replace any statements of the form sum(u.^2) by sum(u.*conj(u)); with u being a, b, or x-y, x+y.

1 件のコメント

Same comparison and observe the robustness
a = 1e-10 % start with a very small angle
a = 1.0000e-10
u = 4*[1 0 0] % arbitrary non-unit vector in X direction
u = 1×3
4 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
v = 5*[cos(a) sin(a) 0] % vector different from u by small angle
v = 1×3
5.0000 0.0000 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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 = 1.0000e-10
nu = norm(u);
nv = norm(v);
xx = u*nv;
yy = v*nu;
a = xx-yy;
b = xx+yy;
theta = 2*atan(sqrt(sum(a.^2)/sum(b.^2)))
theta = 1.0000e-10

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Elementary Math についてさらに検索

製品

リリース

R13SP1

Community Treasure Hunt

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

Start Hunting!

Translated by