Angles between 3D vectors in groups

1 回表示 (過去 30 日間)
Yuntian Guan
Yuntian Guan 2016 年 6 月 27 日
コメント済み: Yuntian Guan 2016 年 6 月 27 日
So I understand that in order to return an angle between two vectors a and b, this code should be used:
angle = atan2d(norm(.cross(a,b),.dot(a,b)));
However, I have vectors in groups (in one variable), and I'd like to be able to perform this as well. Let me clarify, suppose it looks like this:
A = rand(3,10);
B = rand(3,10);
%both the variables have 3*10 elements, but actually they are a total of 10 coordinates(x,y,z)
%where 1st row are all x values, 2nd row y values and last row z values;
%thus for example 1st column of A stands for the first point of A, and etc;
Is there a way to calculate, in groups, the angles between 1st columns of A and B, 2nd columns of A and B, ..., and 10th columns of A and B? The result I'm looking for may need to be a 1*10 variable.
(My thought is to reshape the variables A and B to make it more calculable, but wasn't sure how; or, to create a loop that repeats the calculation for each columns?)
Thanks in advanced,

採用された回答

Roger Stafford
Roger Stafford 2016 年 6 月 27 日
It is not necessary to rearrange your A and B arrays. Just use the three-argument forms of ‘cross’ and ‘dot’, so they operate along the first dimension. (Actually ‘cross’ will do that by default on any dimension of length 3, but you might as well provide for all possible column widths.)
angles = atan2(sqrt(sum(cross(A,B,1).^2,1)),dot(A,B,1));
You will, however, have to compute the corresponding square root of sum of squares along the columns instead of using ’norm’.
I have used 'atan2' for radian measure. I think you want 'atan2d' for degrees.
  1 件のコメント
Yuntian Guan
Yuntian Guan 2016 年 6 月 27 日
Thank you! This works!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by