How can I find the angle between two vectors, including directional information?
Show older comments
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 Comment
AKASH KUMAR
on 19 May 2024
Edited: AKASH KUMAR
on 20 May 2024
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.
Accepted Answer
More Answers (2)
Yashar Farajpour
on 17 Apr 2020
Edited: Yashar Farajpour
on 17 Apr 2020
0 votes
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
on 19 Dec 2022
a = atan2d(x1*y2-y1*x2,x1*x2+y1*y2);
Categories
Find more on Marine and Underwater Vehicles in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!