Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# knnsearch

Class: KDTreeSearcher

Find k-nearest neighbors using KDTreeSearcher object

## Syntax

IDX = knnsearch(NS,Y)
[IDX,D] = knnsearch(NS,Y)
[IDX,D] = knnsearch(NS,Y,'Name',Value)

## Description

IDX = knnsearch(NS,Y) finds the nearest neighbor (closest point) in NS.X for each point in Y. Rows of Y correspond to observations and columns correspond to features. Y must have the same number of columns as NS.X. IDX is a column vector with ny rows, where ny is the number of rows in Y. Each row in IDX contains the index of observation in NS.X which has the smallest distance to the corresponding observation in Y.

[IDX,D] = knnsearch(NS,Y) returns a column vector D containing the distances between each observation in Y and the corresponding closest observation in NS.X. That is, D(i) is the distance between NS.X(IDX(i),:) and Y(i,:).

[IDX,D] = knnsearch(NS,Y,'Name',Value) accepts one or more comma-separated name/value pairs. Specify Name inside single quotes.

## Input Arguments

### Name-Value Pair Arguments

 'K' A positive integer, k, specifying the number of nearest neighbors in NS.X for each point in Y. Default is 1. IDX and D are ny-by-k matrices. D sorts the distances in each row in ascending order. Each row in IDX contains the indices of the k closest neighbors in NS.X corresponding to the k smallest distances in D. 'Distance' Select one of the following distance algorithms.'euclidean' — Euclidean distance (default).'cityblock' — City block distance.'chebychev' — Chebychev distance (maximum coordinate difference).'minkowski' — Minkowski distance.Default is NS.Distance. For more information on these distance metrics, see Distance Metrics. 'IncludeTies' A logical value indicating whether knnsearch includes all the neighbors whose distance values are equal to the Kth smallest distance. If IncludeTies is true, knnsearch includes all these neighbors. In this case, IDX and D are ny-by-1 cell arrays. Each row in IDX and D contains a vector with at least K numeric numbers. D sorts the distances in each vector in ascending order. Each row in IDX contains the indices of the closest neighbors corresponding to these smallest distances in D. Default: false 'P' A positive scalar, p, indicating the exponent of the Minkowski distance. This parameter is only valid whenthe Distance is 'minkowski'. Default is NS.DistParameter if NS.Distance is 'minkowski' and 2 otherwise.

## Examples

Create a KDTreeSearcher object specifying 'minkowski' as the distance metric with an exponent of 5. Perform a k-nearest neighbors search on the object using the chebychev metric and compare the results:

```load fisheriris
x = meas(:,3:4);
kdtreeNS = KDTreeSearcher(x,'Distance','minkowski','P',5)

kdtreeNS =

KDTreeSearcher

Properties:
BucketSize: 50
X: [150x2 double]
Distance: 'minkowski'
DistParameter: 5

% Perform a knnsearch between X and a query point, using
% first Minkowski then Chebychev distance metrics:
newpoint = [5 1.45];
[n,d]=knnsearch(kdtreeNS,newpoint,'k',10);
[ncb,dcb] = knnsearch(kdtreeNS,newpoint,'k',10,...
'distance','chebychev');

% Visualize the results of the two different nearest
% neighbors searches:

% First plot the training data:
gscatter(x(:,1),x(:,2),species)
% Zoom in on the points of interest:
set(gca,'xlim',[4.5 5.5],'ylim',[1 2]); axis square
% Plot an X for the query point:
line(newpoint(1),newpoint(2),'marker','x','color','k',...
'markersize',10,'linewidth',2,'linestyle','none')
% Use circles to denote the Minkowski nearest neighbors:
line(x(n,1),x(n,2),'color',[.5 .5 .5],'marker','o',...
'linestyle','none','markersize',10)
% Use pentagrams to denote the Chebychev nearest neighbors:
line(x(ncb,1),x(ncb,2),'color',[.5 .5 .5],'marker','p',...
'linestyle','none','markersize',10)
legend('setosa','versicolor','virginica','query point',...
'minkowski','chebychev')
set(legend,'location','best')```

## Algorithms

For information on a specific search algorithm, see Distance Metrics.

## References

[1] Friedman, J. H., Bentely, J., and Finkel, R. A. (1977). An Algorithm for Finding Best Matches in Logarithmic Expected Time, ACM Transactions on Mathematical Software 3, 209.