FUNCTION REFERENCE
knnsearch
Brute-force nearest neighbors of query rows.
Brute-force nearest neighbors of query rows.
Syntax
[Idx,D] = knnsearch(X,Y,'K',k,'Distance',metric)K defaults to 1; metrics match pdist2. P, Scale and Cov supply distance parameters. Results have rows(Y)-by-K shape and sort by distance with ascending X indices for ties. IncludeTies=true returns column cell arrays of row vectors including all neighbors at the Kth distance. SortIndices is accepted; exhaustive search always sorts. X rows containing NaN are ignored while retaining original indices. NaN query rows return NaNs, or empty cells with IncludeTies.
Example
[i,d] = knnsearch([0;2;4],[1;3],'K',2); % i = [1 2;2 3]Differences from MATLAB: Exhaustive chunked search only, no kd-tree or searcher objects. Full pair counts and result sizes are bounded before allocation.
These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.