K Nearest Neighbors
by Ani
11 Jul 2007
(Updated 26 Mar 2009)
Program to find the k - nearest neighbors (kNN) within a set of points.
|
Watch this File
|
| File Information |
| Description |
Program to find the k - nearest neighbors (kNN) within a set of points.
Distance metric used: Euclidean distance
Usage:
[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
Example:
a = [1 1; 2 2; 3 2; 4 4; 5 6];
b = [1 1; 2 1; 6 2];
[neighbors distances] = kNearestNeighbors(a,b,2);
Output:
neighbors =
1 2
1 2
4 3
distances =
0 1.4142
1.0000 1.0000
2.8284 3.0000 |
| MATLAB release |
MATLAB 7.5 (R2007b)
|
|
Tags for This File
|
| Everyone's Tags |
|
| Tags I've Applied |
|
| Add New Tags |
Please login to tag files.
|
| Updates |
| 16 Jul 2007 |
I am not modifying the file. Just moving it to the Statistics and Probability section. |
| 24 Mar 2009 |
Users must not include a separate query matrix. This allows more input flexibility. |
| 26 Mar 2009 |
Users must NOW include a separate query matrix. This allows more input flexibility. |
|
Contact us at files@mathworks.com