Solved

Nearest neighbour algorithm

Posted on 2002-07-28
5
379 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

19 Experts available now in Live!

Get 1:1 Help Now