Find knearest neighbors using data
IDX = knnsearch(X,Y)
[IDX,D] = knnsearch(X,Y)
[IDX,D] = knnsearch(X,Y,'Name
',Value
)
IDX = knnsearch(X,Y)
finds the nearest
neighbor in X
for each point in Y
. IDX
is
a column vector with my rows. Each row in IDX
contains
the index of nearest neighbor in X
for the corresponding
row in Y
.
[IDX,D] = knnsearch(X,Y)
returns an myby1
vector D
containing the distances between each
observation in Y
and the corresponding closest
observation in X
. That is, D(i)
is
the distance between X(IDX(i),:)
and Y(i,:)
.
[IDX,D] = knnsearch(X,Y,'
accepts
one or more optional commaseparated namevalue pair arguments. Specify Name
',Value
)Name
inside
single quotes.
knnsearch
does not save a search object.
To create a search object, use createns
.

An mxbyn numeric matrix.
Rows of 

An mybyn numeric matrix
of query points. Rows of 
Specify optional commaseparated pairs of Name,Value
arguments.
Name
is the argument
name and Value
is the corresponding
value. Name
must appear
inside single quotes (' '
).
You can specify several name and value pair
arguments in any order as Name1,Value1,...,NameN,ValueN
.

Positive integer specifying the number of nearest neighbors
in 

A logical value indicating whether Default: 

Nearest neighbors search method. Value is either:


A character vector or a function handle specifying the distance metric. The value can be one of the following:
For more information on these distance metrics, see Distance Metrics. 

A positive scalar, p, indicating the exponent
of the Minkowski distance. This parameter is only valid if the 

A positive definite matrix indicating the covariance matrix
when computing the Mahalanobis distance. This parameter is only valid
when 

A vector 

The maximum number of data points in the leaf node of the kdtree. This argument is only meaningful when using the kdtree search method. Default is 50. 
knnsearch
is
the object function of ExhaustiveSearcher
and KDTreeSearcher
models for
a knearest neighbors search. If you set the NSMethod
namevalue
pair argument to the appropriate value ('exhaustive'
for
an exhaustive search or 'kdtree'
for a Kdtree),
then the search results are equivalent to conducting a distance search
using knnsearch
and without using model objects.
[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.