Solved

Nearest neighbour algorithm

Posted on 2002-07-28
5
381 Views
Last Modified: 2010-03-31
Hi,

I'm trying to write a 'nearest neighbour algorithm' which basically finds a certain number of values in an array that are the closest to a certain value. For example, if I have an array of values:

18, 23, 22, 25, 20, 19, 18, 19, 25

I want to get the 6 values that have the least difference between the value 20. So in this case it would be the values:

18, 18, 19, 19, 22, 23

The first thing I did was to get the differences between each value and 20:

public void kneighbour(int[] values) {
         
  int number = values.length;
  float one = -1;
  float middle;
  float[] diff = new float[values.length];
           
  for(int i = 0; i< number; i++) {
     diff[i] = values[number/2] - values[i];
     if(diff[i] < 0) {
     diff[i] = diff[i]*one;
     }
    System.out.println("This difference is : " + diff[i]);
  }

Once I have all the differences how would I go about getting the six least differences??

Guster

0
Comment
Question by:Guster
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:klf
ID: 7183509
sort the differences in descending order.
0
 
LVL 1

Accepted Solution

by:
PapaDanielle earned 50 total points
ID: 7183897
>>Once I have all the differences how would I go about
>> getting the six least differences??

sort the differences in ascending order , ie from least to greatest ...
0
 

Author Comment

by:Guster
ID: 7183927
how would I be able to relate those differences back to their original array values??
0
 

Author Comment

by:Guster
ID: 7183934
I think I got it
0
 
LVL 1

Expert Comment

by:PapaDanielle
ID: 7183982
OK. Glad to be of help.

Feel free to pass on any other prob.

Cheers

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java Filesystem manipulation of Word Docx file (find replace text) 3 67
Java and GPO 11 71
compre toata in where clue oracle 4 57
couple of eclipse 5 27
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now