繁体   English   中英

绘制3D线,matlab

[英]plot 3D line, matlab

我的问题非常标准,但无法找到解决方案。

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

我正在使用下面给出的功能(和Thanx 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是min。 距原点的距离。

现在我的问题是如何在3D中绘制最佳过滤线。

它有助于实际读取函数的内容,该函数使用奇异值分解。

% 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(),在这种情况下你只需要一对点。 由于定义的行是无限的,因此您需要确定它应该从哪里开始和结束(取决于应用程序)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM