如何在Matlab中以AB和BC线为单位计算角度?
答案 0 :(得分:0)
这是一个不寻常的请求,但是下面的代码可以解决问题:)。
% X and Y coordinates of your time series
X = (1:9)';
Y = [-3;-1;-1.2;-4.5;-5.5;.2;-2;-.5;-.4];
angle_rad = zeros(size(X,1)-2,1); % Preallocate the angle vector
for i = 1:(size(Y,1)-2) % Loop over all vector pairs
% Vectors:
a = [X(i+1)-X(i);Y(i+1)-Y(i)]; % first vector
b = [X(i+2)-X(i+1);Y(i+2)-Y(i+1)]; % second vector
% Vector properties:
dot_prod = a'*b;
l_a = norm(a); % length of first vector
l_b = norm(b); % length of second vector
% Angle:
angle_rad(i) = pi - acos(dot_prod/(l_a*l_b)); % [rad]
end
angle_deg = angle_rad.*180./pi; % [degrees]
看看这是否可行,但是这段代码有点笨拙,也许如果我们知道您使用它的目的,那么还有更好的选择吗?