4.42857

4.4 | 7 ratings Rate this file 191 Downloads (last 30 days) File Size: 1.35 KB File ID: #15562

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.
Comments and Ratings (13)
13 Jul 2007 John D'Errico

Why would this be placed in the linear algebra category?

25 Feb 2008 Yiji Lin

Very helpful! Easy understanding!

08 Apr 2008 Brijesh Shah

easy but effective.thankx

08 Jun 2008 Wenwei Huang

Helpful. Thanks.

23 Sep 2008 yan ziye

what is the sortpos() function in this m file

30 Nov 2008 andy chen

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

05 Sep 2009 Li

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

08 Dec 2009 Sravanti

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

08 Jan 2010 Miguel Torres

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

http://people.revoledu.com/kardi/tutorial/KNN/KNN_Numerical-example.html

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.

14 Oct 2010 Avi

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

29 Jun 2011 Pototo

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!!!!!!!

09 Sep 2011 djanahana djanahana

thank's a lot

Please login to add a comment or rating.
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.

Tag Activity for this File
Tag Applied By Date/Time
statistics Ani 22 Oct 2008 09:19:07
probability Ani 22 Oct 2008 09:19:07
k nearest neighbor Ani 22 Oct 2008 09:19:07
nearest neighbors Ani 22 Oct 2008 09:19:07
k nearest neighbor andy chen 23 Nov 2008 11:08:16
can you help me andy chen 30 Nov 2008 04:51:20
what is sortpos and sortval andy chen 30 Nov 2008 04:51:20
nearest neighbors Sravanti 25 Nov 2009 06:49:41
can you help me sun peng 06 Jul 2010 08:06:09
nearest neighbors Abdulbasit Al-Talabani 12 Jul 2011 05:14:17

Contact us at files@mathworks.com