Documentation |
IDX = knnsearch(NS,Y)
[IDX,D] = knnsearch(NS,Y)
[IDX,D] = knnsearch(NS,Y,'Name',Value)
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 pair arguments. Specify Name inside single quotes.
NS |
KDTreeSearcher object, constructed using KDTreeSearcher or createns. |
Y |
my-by-n numeric matrix, where each row represents one n-dimensional point. The number of columns n must equal the number of columns in NS.X. |
Specify optional comma-separated 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.
'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.
Default is NS.Distance. For details 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 when the Distance is 'minkowski'. Default is NS.DistParameter if NS.Distance is 'minkowski' and 2 otherwise. |