简体   繁体   English

绘制3D线,matlab

[英]plot 3D line, matlab

My question is pretty standard but can't find a solution of that. 我的问题非常标准,但无法找到解决方案。

I have points=[x,y,z] and want to plot best fit line. 我有点= [x,y,z]并且想要绘制最佳拟合线。

I am using function given below (and Thanx Smith) 我正在使用下面给出的功能(和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)

I have a. 我有一个。 So equation may be 等式可能是

points*a+dist=0

where dist is min. dist是min。 distance from origon. 距原点的距离。

Now my question is how to plot best filt line in 3D. 现在我的问题是如何在3D中绘制最佳过滤线。

It helps to actually read the content of the function, which uses Singular Value Decomposition. 它有助于实际读取函数的内容,该函数使用奇异值分解。

% 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);

The best orthogonal fitting line is 最好的正交拟合线是

P = x0 + a.*t P = x0 + a。* t

as the parameter t varies. 因为参数t变化。 This is the direction of maximum variation which means that variation in the orthogonal direction is minimum. 这是最大变化的方向,这意味着正交方向的变化最小。 The sum of the squares of the points' orthogonal distances to this line is minimized. 将点与该线的正交距离的平方和最小化。

This is distinct from linear regression which minimizes the y variation from the line of regression. 这与线性回归不同,线性回归最小化了回归线的y变化。 That regression assumes that all errors are in the y coordinates, whereas orthogonal fitting assumes the errors in both the x and y coordinates are of equal expected magnitudes. 该回归假设所有误差都在y坐标中,而正交拟合假设x和y坐标中的误差具有相等的预期幅度。

[Credit: Roger Stafford , http://www.mathworks.com/matlabcentral/newsreader/view_thread/294030] [图片来源:Roger Stafford,http://www.mathworks.com/matlabcentral/newsreader/view_thread/294030]

Then you only need to create some t and plot it: 然后你只需要创建一些t并绘制它:

for t=0:100,
P(t,:) = x0 + a.*t;
end
scatter3(P(:,1),P(:,2),P(:,3));

You may want to use plot3() instead, in which case you need only a pair of points. 你可能想要使用plot3(),在这种情况下你只需要一对点。 Since a line is infinite by definition, it is up to you to determine where it should begin and end (depends on application). 由于定义的行是无限的,因此您需要确定它应该从哪里开始和结束(取决于应用程序)。

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

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