Documentation 
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,'Name',Value) accepts one or more optional commaseparated namevalue pair arguments. Specify Name inside single quotes.
knnsearch does not save a search object. To create a search object, use createns.
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.
'K' 
Positive integer specifying the number of nearest neighbors in X for each point in Y. Default is 1. IDX and D are mybyK matrices. D sorts the distances in each row in ascending order. Each row in IDX contains the indices of the K closest neighbors in X corresponding to the K smallest distances in D. 
'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 myby1 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 
'NSMethod' 
Nearest neighbors search method. Value is either:

'Distance' 
A string 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. 
'P' 
A positive scalar, p, indicating the exponent of the Minkowski distance. This parameter is only valid if the Distance is 'minkowski'. Default is 2. 
'Cov' 
A positive definite matrix indicating the covariance matrix when computing the Mahalanobis distance. This parameter is only valid when Distance is 'mahalanobis'. Default is nancov(X). 
'Scale' 
A vector S containing nonnegative values, with length equal to the number of columns in X. Each coordinate of X and each query point is scaled by the corresponding element of S when computing the standardized Euclidean distance. This argument is only valid when Distance is 'seuclidean'. Default is nanstd(X). 
'BucketSize' 
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. 
[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.