绘制3D线条,Matlab

3

我的问题很普通,但找不到解决方案。

我有点集 points=[x,y,z],想要绘制最佳拟合直线。

我正在使用下面的函数(感谢 Smith)

% 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是距离原点的最小距离。

现在我的问题是如何在三维空间中绘制最佳拟合线。

1个回答

2

阅读函数的内容有助于理解,该函数使用奇异值分解。

% 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坐标的误差具有相等的期望值。

[来源:Roger Stafford,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(),这样您只需要一对点。由于线的定义是无限的,因此您需要确定它在哪里开始和结束(取决于应用程序)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接