필터 지우기
필터 지우기

How can I find the angle between two vectors, including directional information?

조회 수: 274 (최근 30일)
Hello, I am a graduate student, and I am working on a script that tracks the position of animals during a courtship. I have position data in the form of XY coordinates from two points on each animal's body taken from top down filming. I use these two points to create a vector that defines the animal's orientation. My script needs to calculate the angle between these two vectors, but also include directional information - IE, go from -180 through 0 to 180 degrees, depending on where the vectors are placed (see image).
This is the code that I currently have. It gives me the desired angle (I believe), but is NOT directional. 60 degrees to either side spits out as 60 degrees no matter which it is.
angle_maleToFemale_radians = acos(dot(maleFemaleVector,femaleVector)/(norm(maleFemaleVector)*norm(femaleVector))); angle_maleToFemale_degrees(index) = radtodeg(angle_maleToFemale_radians); angle_maleToFemale_degrees(index) = 180 - angle_maleToFemale_degrees(index);
  댓글 수: 1
AKASH KUMAR
AKASH KUMAR 2024년 5월 19일
편집: AKASH KUMAR 2024년 5월 20일
based on above equation, the angle between two vectors in the range of 0 to 2pi can be found using atan2(Y,X), as described by others in the earlier comments.

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

채택된 답변

Roger Stafford
Roger Stafford 2015년 2월 24일
편집: Roger Stafford 2015년 2월 24일
If v1 = [x1,y1] and v2 = [x2,y2] are the components of two vectors, then
a = atan2d(x1*y2-y1*x2,x1*x2+y1*y2);
gives the angle in degrees between the vectors as measured in a counterclockwise direction from v1 to v2. If that angle would exceed 180 degrees, then the angle is measured in the clockwise direction but given a negative value. In other words, the output of 'atan2d' always ranges from -180 to +180 degrees.
One further observation: Besides the greater range of 'atan2d' as compared with 'acosd', the former does not suffer the inaccuracies that occur with 'acosd' for angles near zero and 180 degrees.
  댓글 수: 14
Cenker Canbulut
Cenker Canbulut 2020년 7월 31일
편집: Cenker Canbulut 2020년 8월 3일
Executed formula and works like charm! Thank you math! Thank you Roger Stafford!

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

추가 답변 (2개)

Yashar Farajpour
Yashar Farajpour 2020년 4월 17일
편집: Yashar Farajpour 2020년 4월 17일
You can use subspace function.
A = [x1,y1,z1];
B = [x2,y2,z2];
Angle = subspace(A',B')
%transposed! they must be column vectors

shantanu  kumar
shantanu kumar 2022년 12월 19일
a = atan2d(x1*y2-y1*x2,x1*x2+y1*y2);

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by