No License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
K Nearest Neighbors

4.4 | 8 ratings Rate this file 123 Downloads (last 30 days) File Size: 1.35 KB File ID: #15562 Version: 1.4

K Nearest Neighbors


Ani (view profile)


11 Jul 2007 (Updated )

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

| Watch this File

File Information

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);

neighbors =
     1 2
     1 2
     4 3

distances =
         0 1.4142
    1.0000 1.0000
    2.8284 3.0000


This file inspired Vector Field Tb, 4 Nearest Neighbor For English Letter Recognition Using Randomized Partitioning., 4 Nearest Neighbor On Iris Recognition Using Randomized Partitioning., and K Nearest Neighbor For English Letter Classification By Doing Random Partition..

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (17)
19 Jul 2014 NIHARIKA

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

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

Comment only
24 May 2014 yasser

yasser (view profile)

29 Jan 2014 putria

putria (view profile)

how to use KNN in one-dimension feature?

Comment only
31 Oct 2013 Clarence son

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

Comment only
09 Sep 2011 djanahana djanahana

thank's a lot

29 Jun 2011 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???
thanks in advance!!!!!!!

Comment only
14 Oct 2010 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))

10 May 2010 Ridha Samosir

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.

Comment only
08 Jan 2010 Miguel Torres

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

Comment only
08 Dec 2009 Sravanti

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

05 Sep 2009 Li

Li (view profile)

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

Comment only
30 Nov 2008 andy chen

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

Comment only
23 Sep 2008 yan ziye

what is the sortpos() function in this m file

08 Jun 2008 Wenwei Huang

Helpful. Thanks.

08 Apr 2008 Brijesh Shah

easy but effective.thankx

25 Feb 2008 Yiji Lin

Very helpful! Easy understanding!

13 Jul 2007 John D'Errico

Why would this be placed in the linear algebra category?

Comment only
24 Mar 2009 1.1

Users must not include a separate query matrix. This allows more input flexibility.

26 Mar 2009 1.4

Users must NOW include a separate query matrix. This allows more input flexibility.

Contact us