Angle between multiple unit vectors contained within a matrix.
12 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Dyuman Joshi
on 14 Feb 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 Comments
More Answers (1)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!