我的问题很标准,但找不到解决方案。
我有points=x,y,z,并且想要绘制最佳拟合线。
我正在使用下面给出的函数(还有Thanx )
% LS3DLINE.M Least-squares line in 3 dimensions.
%
% Version 1.0
% Last amended I M Smith 27 May 2002.
% Created I M Smith 08 Mar 2002
% ---------------------------------------------------------------------
% Input
% X Array [x y z] where x = vector of x-coordinates,
% y = vector of y-coordinates and z = vector of
% z-coordinates.
% Dimension: m x 3.
%
% Output
% x0 Centroid of the data = point on the best-fit line.
% Dimension: 3 x 1.
%
% a Direction cosines of the best-fit line.
% Dimension: 3 x 1.
%
% <Optional...
% d Residuals.
% Dimension: m x 1.
%
% normd Norm of residual errors.
% Dimension: 1 x 1.
% ...>
%
% [x0, a <, d, normd >] = ls3dline(X)我有一个。所以方程式可能是
points*a+dist=0其中dist是最小。到原点的距离。
现在我的问题是如何在3D中绘制最好的滤波线。
发布于 2012-06-04 17:15:36
它有助于实际读取使用奇异值分解的函数的内容。
% 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(),在这种情况下,您只需要一对点。由于根据定义,行是无限的,因此由您决定它应该从哪里开始和结束(取决于应用程序)。
https://stackoverflow.com/questions/10878167
复制相似问题