|
-
Jul 30th, 2010, 11:48 AM
#1
Thread Starter
Lively Member
[RESOLVED] Find Closest Match
I have a list of numbers:
Code:
100,125,150,175,200,225,250,300,350,400,450,500,600,700,800,900,1000
I do some maths and get a result. I need to workout somehow which one of those numbers above is closest to the RESULT number.
For example if the result was 620 it would be closer to 600 but if it was 678 it would be closer to 700.
Any suggestions?
-
Jul 30th, 2010, 11:56 AM
#2
Re: Find Closest Match
Sort + modified binary search.
If you want something better, then by all means supply more information about the 'some maths' that you're planning to do.
-
Jul 30th, 2010, 12:02 PM
#3
Re: Find Closest Match
How the number is arrived at is irrelevant... what is relevant is the result: 620....
To get the closest number first you need to find the two numbers on either end... which should be simple with a binary search, or a simple loop until where valueArray(i) <= 620 and valueArray(i+1) > 620.... that gives you the 600 and 700... Then all you need to do is take the difference between your end numbers and your number and the one with the lowest difference is your closer number.
620 - 600 = 20
700- 620 = 80
since 20 < 80... 600 is your closer number.
-tg
-
Jul 30th, 2010, 12:15 PM
#4
Re: Find Closest Match
Code:
'create list of numbers
Dim lstOnums As New List(Of Integer)
lstOnums.AddRange(New Integer() {100, 125, 150, 175, 200, 225, 250, 300, 350, 400, 450, 500, 600, 700, 800, 900, 1000})
'add result to the list, and sort
Dim rslt As Integer = 620
lstOnums.Add(rslt)
lstOnums.Sort()
'get the index of the result.
Dim idx As Integer = lstOnums.IndexOf(rslt)
'a couple of checks here and you should have your answer
-
Jul 30th, 2010, 12:46 PM
#5
Re: Find Closest Match
try this:
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim numbers() As Integer = {100, 125, 150, 175, 200, 225, 250, 300, 350, 400, 450, 500, 600, 700, 800, 900, 1000}
Dim closest As Integer
Dim closestDifference As Integer = Integer.MaxValue
Dim inputNumber As Integer
If Integer.TryParse(TextBox1.Text, inputNumber) Then
For x As Integer = 0 To numbers.GetUpperBound(0)
If Math.Abs(inputNumber - numbers(x)) < closestDifference Then
closestDifference = Math.Abs(inputNumber - numbers(x))
closest = numbers(x)
End If
Next
MsgBox(closest)
End If
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 30th, 2010, 01:33 PM
#6
Re: Find Closest Match
 Originally Posted by techgnome
How the number is arrived at is irrelevant... what is relevant is the result: 620....
That's a little short-sighted. How the number is reached is NOT irrelevant if the closest number can be determined within the algorithm that creates the result. This can potentially reduce complexity from O(n) to O(log_n), which is significant for larger data sets.
-
Jul 30th, 2010, 02:00 PM
#7
Re: Find Closest Match
The user enters the number.
The user enters a number, and it's doubled.
It generates a random number between 1 and 12million, the ARC and COS is taken, summed, squared, cubed-root.
If the requirement it to take a number as an input and, given a set of other numbers, find the closest... do we care how that input number was derived? maybe in the large scheme things, but for the sake of arguement, let's say that this "nearest" function IS being inserted in to said algorithm you mentioned... after all... that IS what we are trying to do here, right? find the closest number? for purposes of the function, we don't care how it gets called or where from. All the function should be concerned about is, given a set of numbers, and an input number, find the closest from the list.
-tg
-
Jul 30th, 2010, 02:36 PM
#8
Re: Find Closest Match
Code:
Module module1
Dim aRnd As New Random
Public Sub Main()
'this
'lstOnums.AddRange(New Integer() {100, 125, 150, 175, 200, 225, 250, 300, 350, 400, 450, 500, 600, 700, 800, 900, 1000})
'or this
For x As Integer = 100 To 1000 Step 25
lstOnums.Add(x)
Next
'
'
Dim rslt As Integer = 100
Console.WriteLine("The closest to {0} is {1}", rslt, closest(rslt))
rslt = 1000
Console.WriteLine("The closest to {0} is {1}", rslt, closest(rslt))
Dim stpw As New Stopwatch
For x As Integer = 1 To 20
rslt = aRnd.Next(100, 1001)
stpw.Reset()
stpw.Start()
Dim clsts As Integer = closest(rslt)
stpw.Stop()
Console.WriteLine("The closest to {0} is {1} in {2} ticks", rslt, clsts, stpw.ElapsedTicks)
Next
Console.ReadLine()
End Sub 'Main
Dim lstOnums As New List(Of Integer)
Private Function closest(ByVal rslt As Integer) As Integer
'create list of numbers
'add result to the list, and sort
lstOnums.Add(rslt)
lstOnums.Sort()
'get the index of the result.
Dim idx As Integer = lstOnums.IndexOf(rslt)
'a couple of checks here and you should have your answer
Dim rv As Integer
If idx = 0 Then 'is it first item in list
rv = lstOnums(1)
Else 'determine which neighbor is closest
Dim dl As Integer = lstOnums(idx) - lstOnums(idx - 1)
Dim dh As Integer = lstOnums(idx + 1) - lstOnums(idx)
If dl >= dh Then rv = lstOnums(idx + 1) Else rv = lstOnums(idx - 1)
End If
lstOnums.RemoveAt(idx) 'remove the number we added
Return rv
End Function
End Module
-
Jul 30th, 2010, 05:39 PM
#9
Re: Find Closest Match
Well, is the array originally sorted? Sorting it, then performing a binary search would be less efficient than just looping through like .paul. did...
-
Jul 31st, 2010, 07:14 AM
#10
Thread Starter
Lively Member
Re: Find Closest Match
This works perfectly! Thank you!!!
 Originally Posted by .paul.
try this:
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim numbers() As Integer = {100, 125, 150, 175, 200, 225, 250, 300, 350, 400, 450, 500, 600, 700, 800, 900, 1000}
Dim closest As Integer
Dim closestDifference As Integer = Integer.MaxValue
Dim inputNumber As Integer
If Integer.TryParse(TextBox1.Text, inputNumber) Then
For x As Integer = 0 To numbers.GetUpperBound(0)
If Math.Abs(inputNumber - numbers(x)) < closestDifference Then
closestDifference = Math.Abs(inputNumber - numbers(x))
closest = numbers(x)
End If
Next
MsgBox(closest)
End If
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|