在Matlab中获取时间序列的两条线之间的角度
Get angle between two lines of a time series in Matlab
如何在 Matlab 中计算线 AB 和 BC 之间的角度(以度为单位)?
Time-series
这是一个有点不寻常的请求,但下面的代码应该可以解决问题:)。
% 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]
看看这是否有效,但这段代码有点笨拙,如果我们知道你用它做什么,也许会有更好的选择?
如何在 Matlab 中计算线 AB 和 BC 之间的角度(以度为单位)?
Time-series
这是一个有点不寻常的请求,但下面的代码应该可以解决问题:)。
% 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]
看看这是否有效,但这段代码有点笨拙,如果我们知道你用它做什么,也许会有更好的选择?