File Exchange

## K Nearest Neighbors

version 1.4 (1.35 KB) by

Program to find the k - nearest neighbors (kNN) within a set of points.

3.81818
10 Ratings

Updated

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

Thanks.

Chuck37

### Chuck37 (view profile)

This thing is pure brute force. Not suitable for large data sets and/or numbers of queries.

NIHARIKA

### NIHARIKA (view profile)

i am not able to run this file. It shows the following error:

Error using kNearestNeighbors (line 29)
Not enough input arguments.

yasser

putria

### putria (view profile)

how to use KNN in one-dimension feature?

Clarence son

### Clarence son (view profile)

i need code for intrusion detection using k-nearest neighbor and neural network in matlab. thanks

djanahana djanahana

thank's a lot

Pototo

### Pototo (view profile)

hello, I am imputing some square matrices, but this algorithm is not working. I though that as long as D is the same for both matrices, this would work. Does that mean that N cannot be equal to M at all???

Avi

### Avi (view profile)

good efficient code , can be added group so return group selected by the indexes and make majority voting with distance weight

Ydec = ArgMax(group(i)/Dist(i))

Ridha Samosir

### Ridha Samosir (view profile)

i need code for r-nearest neightbour using sliding window concept start form window 3x3 then shift until window size of image reached. All of point is grouped based on color similarity using euclidian distance. Parameter for this code is count of r-nearest neightbour ( 25 ) and minimum color distance ( 6 ). Minimum color distance is a treshold for check the similarity between the point. So all of data point which have similarity distance color < 6 is taken for make a group. Start from window 3x3, define inisialization point X then memorizaed all data point ( 25 ) which color distance < 6.

Miguel Torres

### Miguel Torres (view profile)

The square distance is calculated (without square root) . Take a look a this tutorial:

Sravanti

### Sravanti (view profile)

really nice, simple and easy - but don't you need to take the square root for distances?

Li

### Li (view profile)

Is it fast for large dataset? Is it implemented using KD-tree?

andy chen

### andy chen (view profile)

can you tell me what is sortpos() and sortval()

yan ziye

what is the sortpos() function in this m file

Wenwei Huang

Brijesh Shah

easy but effective.thankx

Yiji Lin

John D'Errico

Why would this be placed in the linear algebra category?