簡體   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