# Angle between two 3D vectors

조회 수: 17(최근 30일)
Maciej Kupras 2022년 10월 30일
댓글: Maciej Kupras 2022년 10월 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:
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 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 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 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)
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
%If any issue let me know, be specific please!
##### 댓글 수: 1표시숨기기 없음
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

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

### 범주

Find more on Matrix Indexing in Help Center and File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!