findnearest

PURPOSE ^

FINDNEAREST finds from a array of points the nearest to a certain point.

SYNOPSIS ^

function [nxpts,ixs]=findnearest(p,pts,num,same)

DESCRIPTION ^

 FINDNEAREST finds from a array of points the nearest to a certain point.
 
 FINDNEAREST finds the nearest num points to point p from points pts. If
 same is input, the same point will be returned if it exists in pts.
 
 USAGE:
     [nxpts,ixs]=findnearest(p,pts,num); gets the nearest num pts
 
     [nxpts,ixs]=findnearest(p,pts,num,1); the same point is returned if
     it exists
 
 INPUTS:
     pt: reference point
 
     pts: 2xN array of points
 
     num: number of nearest points required
 
     same: flag, if input allows the function to return point pt
 
 OUTPUTS:
     nxpts: 2xnum array containing the found points
 
     ixs: the column coordinates of the points

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [nxpts,ixs]=findnearest(p,pts,num,same)
0002 % FINDNEAREST finds from a array of points the nearest to a certain point.
0003 %
0004 % FINDNEAREST finds the nearest num points to point p from points pts. If
0005 % same is input, the same point will be returned if it exists in pts.
0006 %
0007 % USAGE:
0008 %     [nxpts,ixs]=findnearest(p,pts,num); gets the nearest num pts
0009 %
0010 %     [nxpts,ixs]=findnearest(p,pts,num,1); the same point is returned if
0011 %     it exists
0012 %
0013 % INPUTS:
0014 %     pt: reference point
0015 %
0016 %     pts: 2xN array of points
0017 %
0018 %     num: number of nearest points required
0019 %
0020 %     same: flag, if input allows the function to return point pt
0021 %
0022 % OUTPUTS:
0023 %     nxpts: 2xnum array containing the found points
0024 %
0025 %     ixs: the column coordinates of the points
0026 
0027 if size(pts,1)>2
0028     pts=pts';
0029 end
0030 dist=sqrt((p(1)-pts(1,:)).^2+(p(2)-pts(2,:)).^2);
0031 % if nargin<4
0032 %     ind=find(dist==0,1);
0033 %     if ~isempty(ind)
0034 %         pts(:,ind)=[NaN;NaN];
0035 %         dist(ind)=NaN;
0036 %     end
0037 % end
0038 
0039 [dist,ix]=sort(dist);
0040 
0041 if nargin<4
0042     ind=find(dist==0,1);
0043     ix(ind)=[];
0044 end
0045 nxpts=pts(:,ix(1:num));
0046 ixs=ix(1:num);

Generated on Sun 04-Apr-2010 17:13:59 by m2html © 2005