필터 지우기
필터 지우기

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

조회 수: 1,062 (최근 30일)
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일
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
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));

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

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

James Tursa
James Tursa 2015년 7월 9일
편집: James Tursa 2019년 1월 5일
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
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R13SP1

Community Treasure Hunt

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

Start Hunting!

Translated by