它有助于实际阅读函数的内容,该函数使用奇异值分解。
% calculate centroid
x0 = mean(X)';
% form matrix A of translated points
A = [(X(:, 1) - x0(1)) (X(:, 2) - x0(2)) (X(:, 3) - x0(3))];
% calculate the SVD of A
[U, S, V] = svd(A, 0);
% find the largest singular value in S and extract from V the
% corresponding right singular vector
[s, i] = max(diag(S));
a = V(:, i);
最好的正交拟合线是
P = x0 + a.*t
随着参数 t 的变化。这是最大变化的方向,这意味着正交方向上的变化最小。点到这条线的正交距离的平方和被最小化。
这与线性回归不同,线性回归最小化回归线的 y 变化。该回归假设所有误差都在 y 坐标中,而正交拟合假设 x 和 y 坐标中的误差具有相同的预期大小。
[图片来源:罗杰·斯塔福德,http://www.mathworks.com/matlabcentral/newsreader/view_thread/294030]
然后你只需要创建一些 t 并绘制它:
for t=0:100,
P(t,:) = x0 + a.*t;
end
scatter3(P(:,1),P(:,2),P(:,3));
您可能想使用plot3(),在这种情况下您只需要一对点。由于一条线根据定义是无限的,因此由您决定它的开始和结束位置(取决于应用程序)。