Angle between multiple unit vectors contained within a matrix.
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Dyuman Joshi
2023-2-14
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))
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!