Program to find the k - nearest neighbors (kNN) within a set of points.
Distance metric used: Euclidean distance
[neighbors distances] = kNearestNeighbors(dataMatrix, queryMatrix, k);
dataMatrix (N x D) - N vectors with dimensionality D (within which we search for the nearest neighbors)
queryMatrix (M x D) - M query vectors with dimensionality D
k (1 x 1) - Number of nearest neighbors desired
a = [1 1; 2 2; 3 2; 4 4; 5 6];
b = [1 1; 2 1; 6 2];
[neighbors distances] = kNearestNeighbors(a,b,2);
Ani (2022). K Nearest Neighbors (https://www.mathworks.com/matlabcentral/fileexchange/15562-k-nearest-neighbors), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Inspired: Vector Field TB, 4-Nearest Neighbor for english letter recognition using randomized partitioning., 4-Nearest Neighbor on iris recognition using randomized partitioning., K-nearest neighbor for English letter classification by doing random partition.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!