Angle Between two vectors.

30 ビュー (過去 30 日間)
Andrew Bass
Andrew Bass 2020 年 4 月 11 日
回答済み: James Tursa 2020 年 4 月 12 日
How could I create a function that will take two vectors as inputs, and outputs the angle between them in radians.

採用された回答

Jim Riggs
Jim Riggs 2020 年 4 月 11 日
編集済み: Jim Riggs 2020 年 4 月 11 日
Given two vectors A and B, the dot product of the two vectors (A dot B) gives the product ABcos(ang), so to get just the angle, you want to take the dot product of two unit vectors;
Assume A = [ax, ay, az], B = [bx, by, bz]
magA = sqrt(ax^2+ay^2+az^2); % = sqrt(A(1)^2 + A(2)^2 + A(3)^2)
magB = sqrt(bx^2+by^2+bz^2); % = sqrt(B(1)^2 + B(2)^2 + B(3)^2)
UA = [ax/magA, ay/magA, az/magA]; % A unit vector, = [A(1)/magA, A(2)/magA, A(3)/magA]
UB = [bx/magB, by/magB, bz/magB]; % B unit vector, = [B(1)/magB, B(2)/magB, B(3)/magB]
cosAng = dot(UA,UB); % = UA(1)*UB(1) + UA(2)*UB(2) + UA(3)*UB(3)
ang = acos(cosang); % This is the angle between vector A and Vector B, in radians
As a function;
function ang = Vangle(A,B)
magA = sqrt(A(1)^2 + A(2)^2 + A(3)^2);
magB = sqrt(B(1)^2 + B(2)^2 + B(3)^2);
UA = [A(1)/magA, A(2)/magA, A(3)/magA]
UB = [B(1)/magB, B(2)/magB, B(3)/magB]
cosAng = UA(1)*UB(1) + UA(2)*UB(2) + UA(3)*UB(3);
ang = acos(cosang);
return
end
  1 件のコメント
Andrew Bass
Andrew Bass 2020 年 4 月 12 日
thank you! this was very helpful

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

その他の回答 (1 件)

James Tursa
James Tursa 2020 年 4 月 12 日

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by