Finding the two closest values of a given value

  • Thread starter Thread starter BubblesAreUs
  • Start date Start date
  • Tags Tags
    Value
Click For Summary
The discussion focuses on finding two values in a sorted list that are closest to a given input value, x, to create an interval for an Intermediate Value Theorem (IVT) problem. Participants suggest calculating the absolute differences between list elements and x to identify the closest values, while also considering whether to find values above and below x. The importance of maintaining the original positions of the values in the list is emphasized, and it is noted that if the list is sorted, a simple linear search can efficiently identify the two closest values. The conversation concludes with a consensus on the method's effectiveness for the given task.
BubblesAreUs
Messages
43
Reaction score
1

Homework Statement



Say, I was given a list, i.e. [1, 3, 7, 9, 12, 15, 24]

Now I'd like to find two values closest to an input ( say, x) so that I can produce an interval ( f(a), f(b)).

This interval would be used in an IVT problem where the intervals will be be part of [a,b] and will be used to compute the output of the initial input.

Homework Equations



given x

a < x < b

Find a and b within the list.

The Attempt at a Solution



I'm trying to tackle this problem via hand, but I'm also using python as well. I've thought of using a list, calculating the absolute difference between each element and then find the lowest difference.

List = [...]
x ( given number) = input("Enter given number")

Output = numpy.substract(List, x)

The lowest absolute difference should represent the closest value to the given number. I suppose I could take the second lowest difference as well, but I haven't really thought about a control loop for that problem.

On MATLAB, it's possible to take an array and given number, compute its absolute value and then use that to find its min, i.e. [,idx]= min(abs(List - x)). On Python, it doesn't seem like min() can be used for arrays/ lists.

Let's see how this goes...
 
Physics news on Phys.org
Do you want to find the closest values or one value above and one below? In your example, what happens with x=16? Do you want to find 12 and 15 (the closest values) or 15 and 24 (to have an interval that contains 16)?

Is the input always sorted like in your example? That makes it easier.

BubblesAreUs said:
I'm trying to tackle this problem via hand, but I'm also using python as well. I've thought of using a list, calculating the absolute difference between each element and then find the lowest difference.
You can keep track of the lowest two differences then you don't have to store all the differences, especially if the list is long.
 
mfb said:
Do you want to find the closest values or one value above and one below? In your example, what happens with x=16? Do you want to find 12 and 15 (the closest values) or 15 and 24 (to have an interval that contains 16)?
If x = 16, I'd want it to fall within the interval: 15 < x < 24. It should intermediate to two points.

Is the input always sorted like in your example? That makes it easier.

You can keep track of the lowest two differences then you don't have to store all the differences, especially if the list is long.
If I store the difference, I will have to fetch the position in the original position as well.

Is there an easier way to just retrieve the lowest absolute difference and then produce the original number? Right now, I'm thinking of utilising a while Output != min(output) with i being tied a counter. That would tell me the index of the list where min is located. Then I could just add the index by 1 since the list will be in ascending order and intermediate to that value and the index where the abs is the lowest.
 
BubblesAreUs said:
If x = 16, I'd want it to fall within the interval: 15 < x < 24. It should intermediate to two points.
Then taking absolute differences does not help. You need the sign.
BubblesAreUs said:
If I store the difference, I will have to fetch the position in the original position as well.
Keep track of the position, too, of course.

Again: Is your original input list sorted by size? Your example would suggest this.
 
mfb said:
Then taking absolute differences does not help. You need the sign.
Keep track of the position, too, of course.
Yes, I could use the index() feature. Now, if I were to cease using the abs() function, I would no longer be able to determine the lowest distance between the given value and the an integer on the list. Since it is a continuous function, either the value to its left-side or right-side would represent its corresponding bond to x.

I was thinking about the squeeze theorem and how the two lower/ upper bound functions and the introduced target function lie in between the two bounds.

Again: Is your original input list sorted by size? Your example would suggest this.[/QUOTE] Yes it'll be sorted by size in ascending order.
 
BubblesAreUs said:
Yes, I could use the index() feature. Now, if I were to cease using the abs() function, I would no longer be able to determine the lowest distance between the given value and the an integer on the list. Since it is a continuous function, either the value to its left-side or right-side would represent its corresponding bond to x.

I was thinking about the squeeze theorem and how the two lower/ upper bound functions and the introduced target function lie in between the two bounds.

Again: Is your original input list sorted by size? Your example would suggest this.
Yes it'll be sorted by size in ascending order.[/QUOTE]
As mfb said, it's pretty simple if the list is sorted. Just go through the list in order until the first item larger than x.That's b, the item in the list just before that is a.

Only small problems to deal with:
x is equal to an item in the list.
x is outside of the range of items in the list.​
 
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
SammyS said:
Yes it'll be sorted by size in ascending order.
As mfb said, it's pretty simple if the list is sorted. Just go through the list in order until the first item larger than x.That's b, the item in the list just before that is a.

Only small problems to deal with:
x is equal to an item in the list.
x is outside of the range of items in the list.​
[/QUOTE] Got it. Seems to work.

Thanks.
 

Similar threads

Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
22
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 27 ·
Replies
27
Views
3K