Angles between 3D vectors in groups
1 view (last 30 days)
Show older comments
Yuntian Guan
on 27 Jun 2016
Commented: Yuntian Guan
on 27 Jun 2016
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,
0 Comments
Accepted Answer
Roger Stafford
on 27 Jun 2016
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.
More Answers (0)
See Also
Categories
Find more on Logical 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!