Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Create *K*d-tree nearest neighbor searcher

`KDTreeSearcher`

model objects store the results of a nearest
neighbor search that uses the *K*d-tree algorithm. Results include the
training data, distance metric and its parameters, and maximum number of data points in
each leaf node (that is, the bucket size). The *K*d-tree algorithm
partitions an *n*-by-*K* data set by recursively
splitting *n* points in *K*-dimensional space into a
binary tree.

Once you create a `KDTreeSearcher`

model object, you can search the
stored tree to find all neighboring points to the query data by performing a nearest
neighbor search using `knnsearch`

or a radius search using
`rangesearch`

. The *K*d-tree
algorithm is more efficient than the exhaustive search algorithm when
*K* is small (that is, *K* ≤ 10), the training and
query sets are not sparse, and the training and query sets have many
observations.

Use either the `createns`

function or the
`KDTreeSearcher`

function (described here) to create a
`KDTreeSearcher`

model object. Both functions use the same syntax
except that the `createns`

function has the `'NSMethod'`

name-value pair
argument, which you use to choose the nearest neighbor search method. The
`createns`

function also creates an `ExhaustiveSearcher`

object. Specify `'NSMethod','kdtree'`

to create a `KDTreeSearcher`

object. The default is
`'kdtree'`

if *K* ≤ 10, the training data is not
sparse, and the distance metric is Euclidean, city block, Chebychev, or
Minkowski.

`Mdl = KDTreeSearcher(X)`

`Mdl = KDTreeSearcher(X,Name,Value)`

grows a default `Mdl`

= KDTreeSearcher(`X`

)*K*d-tree (`Mdl`

) using the
*n*-by-*K* numeric matrix of training data
(`X`

).

specifies additional options using one or more name-value pair arguments. You
can specify the maximum number of data points in each leaf node (that is, the
bucket size) and the distance metric, and set the distance metric parameter
(`Mdl`

= KDTreeSearcher(`X`

,`Name,Value`

)`DistParameter`

)
property. For example,
`KDTreeSearcher(X,'Distance','minkowski','BucketSize',10)`

specifies to use the Minkowski distance when searching for nearest neighbors and
to use `10`

for the bucket size. To specify
`DistParameter`

, use the `P`

name-value pair argument.

`knnsearch` | Find k-nearest neighbors using object |

`rangesearch` | Find all neighbors within specified distance using object |