Angle between multiple unit vectors contained within a matrix.
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rob Malkin
el 14 de Feb. de 2023
Comentada: Dyuman Joshi
el 14 de Feb. de 2023
Hi all,
This should be pretty simple but it's escaping me.
I have two matrices (a and b) which contain 10 unit vectors. I would like to calculate the angle between the ten pairs. When I try the method below it gives me a single value as the norm function normalises the whole matrix.
Any thoughts would be most welcome.
% Example vectors. They won't be the same when used.
a_single = [0,0,1];
b_single = [1,0,1];
a_norm = a_single./norm(a_single);
b_norm = b_single./norm(b_single);
% Repeat for 10 elements
a = repmat(a_norm, [10,1]);
b = repmat(b_norm, [10,1]);
% Calculate angle between them.
cosOfAngle = max(min(dot(a,b)/(norm(a)*norm(b)),1),-1);
angle = real(acosd(cosOfAngle))
0 comentarios
Respuesta aceptada
Dyuman Joshi
el 14 de Feb. de 2023
norm as you have used, outputs the norm of the whole array.
If you want to calculate the individual angles between pairs, use vecnorm to calculate norm of each row corresponding to each (unit) vector and modify the dot product (dimension)
%random data
a_single = rand(10,3)
%normalising each vector i.e. each row
a = a_single./vecnorm(a_single,2,2)
%verifying the norm
sqrt(sum(a(1,:).^2))
%similarly for b
b_single = rand(10,3);
b = b_single./vecnorm(b_single,2,2);
ang = acosd(dot(a,b,2))
2 comentarios
Más respuestas (1)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!