# Angle between two 3D vectors

7 visualizzazioni (ultimi 30 giorni)
Maciej Kupras il 30 Ott 2022
Commentato: Maciej Kupras il 30 Ott 2022
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.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

David Hill il 30 Ott 2022
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 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Maciej Kupras il 30 Ott 2022
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

Accedi per commentare.

### Più risposte (1)

KALYAN ACHARJYA il 30 Ott 2022
Modificato: KALYAN ACHARJYA il 30 Ott 2022
"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 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Maciej Kupras il 30 Ott 2022
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

Accedi per commentare.

### Categorie

Scopri di più su Matrix Indexing in Help Center e File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by