-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfindBestLineThroughPoints.m
More file actions
30 lines (26 loc) · 1017 Bytes
/
Copy pathfindBestLineThroughPoints.m
File metadata and controls
30 lines (26 loc) · 1017 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function [ pt, vec ] = findBestLineThroughPoints( pts )
% Find the line that minimizes the L2 norm of the distances between the points and the line
% The line is parameterized by a point and a vector
%
% [ pt, vec ] = findBestLineThroughPoints( pts )
%
% Inputs:
% pts - an N x M array where N is the number of points and M is the dimension of the point space
%
% Outputs:
% pt - an M element array specifying a point
% vec - a normalized vector specifying the direction of the line
%
% Written by Nicholas Dwork - Copyright 2025
%
% https://github.com/ndwork/dworkLib.git
%
% This software is offered under the GNU General Public License 3.0. It
% is offered without any warranty expressed or implied, including the
% implied warranties of merchantability or fitness for a particular
% purpose.
pt = mean( pts, 1 ); % The mean point serves as the point of the line
centeredPts = bsxfun( @minus, pts, pt );
[~,~,V] = svd( centeredPts, 'econ' );
vec = V(:,1);
end