findknn#
[indxknn, distknn] = findknn(x, y, xpoint, ypoint, numofneighbors, distCalcMethod, dispout)
Description#
Find k-nearest neighbors using Euclidean distance
Inputs#
- x
Coordinate of data in x direction
- y
Coordinate of data in y direction
- xpoint
Coordinate (in x direction) of point that nearest point to that is disired to be found
- ypoint
Coordinate (in y direction) of point that nearest point to that is disired to be found
- numofneighbors=1;
Number of nearest neighbors to (xpoint,ypoint) that are desired to be found
- distCalcMethod=’1d’;
- Distance calculation method‘1d’: use 1d array‘pdist2’: Use 2d distance function‘vector’: Use vectorized distance
- dispout=’no’;
Define to display outputs or not (‘yes’: display, ‘no’: not display)
Outputs#
- indxknn
- Index of nearest neighbors pointsreturns M*N array where M=length(xpoint) and N=numofneighborsnth row associated with nth point in (xpoint,ypoint)
- distknn
- Distance of nearest neighbors pointsreturns M*N array where M=length(xpoint) and N=numofneighborsmth row associated with mth point in (xpoint,ypoint)nth column associated with nth nearest neighbors
Examples#
x(:,1)=10.*rand(100,1);
y(:,1)=10.*rand(100,1);
xpoint=mean(x);
ypoint=mean(y);
[indxknn,distknn]=findknn(x,y,xpoint,ypoint,1,'1d','yes');
x(:,1)=10.*rand(100,1);
y(:,1)=10.*rand(100,1);
xpoint=[2.5;5;7.5];
ypoint=[3;6;9];
[indxknn,distknn]=findknn(x,y,xpoint,ypoint,10,'1d','yes');