Angle between two 3D vectors

7 次查看(过去 30 天)
I do have one column vector:
st_direction =
0.6320
0.7308
0.2579
And the matrix of (let's say) 5 vectors:
A =
-0.8903 -0.6071 -0.7037 0.4638 0.7759
0.3896 0.5431 -0.1126 -0.2208 -0.4987
0.2358 -0.5801 0.7015 -0.8580 0.3863
I need to calculate angles between vector st_direction and each column vector from A matrix, for example:
- angle between vector [0.6320 0.7308 0.2579] and vector [-0.8903, 0.3896, 0.2358].
Currently I'm trying to calculate it like that:
acos((dot(A(:,i),st_direction))/(norm(A(:,i))*norm(st_direction)))
But I think that something is wrong, because in later stages of a code I must check whether this angle is less than given variable:
acos((dot(A(:,i),st_direction))/(norm(A(:,i))*norm(st_direction)))<=deg2rad(fov_st)
I think that something is wrong because the program spits many point that when plotted on a graph are very far away. Is there any mistake?
Below there is an example for a greater amount of column vectors in matrix A (1000, generated as units vectors ) so it's easier to see. Red points are the points that satisfy the equation and the cyan line is the st_direction vector. As you can see, those points seem to be very randomly distributed.

采纳的回答

David Hill
David Hill 2022-10-30
I don't see anything wrong. I assume you are using a loop.
st_direction =[0.6320;0.7308;0.2579];
A =[-0.8903 -0.6071 -0.7037 0.4638 0.7759
0.3896 0.5431 -0.1126 -0.2208 -0.4987
0.2358 -0.5801 0.7015 -0.8580 0.3863];
for k=1:5
Angles(k)=acos((dot(A(:,k),st_direction))/(norm(A(:,k))*norm(st_direction)));
end
Angles
Angles = 1×5
1.7897 1.7076 1.9242 1.6604 1.3433
  1 个评论
Maciej Kupras
Maciej Kupras 2022-10-30
Thank you very much for checking my code. As it turned out in a later loop I mistakenly assigned variables from different loops so wrong colums were placed in a new matrix. So stupid mistake but so common :P

请先登录,再进行评论。

更多回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2022-10-30
编辑:KALYAN ACHARJYA 2022-10-30
"I need to calculate angles between vector st_direction and each column vector from A matrix, for example:"
st_direction =[0.6320 0.7308 0.2579];
A =[-0.8903 -0.6071 -0.7037 0.4638 0.7759
0.3896 0.5431 -0.1126 -0.2208 -0.4987
0.2358 -0.5801 0.7015 -0.8580 0.3863];
angle_data=zeros(1,size(A,2)); % Memory Pre-Allocation
for i=1:size(A,2)
angle_data(i)=atan2(norm(cross(st_direction,A(:,i))),dot(st_direction,A(:,i))); %radians
end
angle_data
angle_data = 1×5
1.7897 1.7076 1.9242 1.6604 1.3433
Angle in Degrees ()
angle_data =
102.5408 97.8392 110.2498 95.1358 76.9647
Use atan2d instead of atan2
%If any issue let me know, be specific please!
  1 个评论
Maciej Kupras
Maciej Kupras 2022-10-30
Thank you very much for checking my code. As it turned out in a later loop I mistakenly assigned variables from different loops so wrong colums were placed in a new matrix. So stupid mistake but so common :P

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by