Finding the two closest values of a given value

  • Thread starter BubblesAreUs
  • Start date
  • Tags
    Value
In summary, in order to find two values closest to a given input in a sorted list, one can iterate through the list until the first value larger than the input is reached. The value before that is the lower bound, while the larger value is the upper bound. Special cases to consider are when the input is equal to an item in the list or when it is outside of the range of items in the list.
  • #1
BubblesAreUs
43
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.​
 
  • #7
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
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.
 

Related to Finding the two closest values of a given value

1. What is the purpose of finding the two closest values of a given value?

The purpose of finding the two closest values of a given value is to determine the values that are closest to a specific number, either on a number line or in a dataset. This can be useful in many scientific and mathematical applications, such as data analysis, optimization, and approximation.

2. How do you find the two closest values of a given value on a number line?

To find the two closest values of a given value on a number line, you can use the method of approximation. Start by identifying the given value and the numbers on either side of it on the number line. Then, compare the distances between the given value and each of the neighboring numbers. The two closest values will be the numbers with the smallest distance from the given value.

3. What is the difference between finding the two closest values and finding the two nearest neighbors?

Finding the two closest values refers to determining the two numbers that are closest to a given value, while finding the two nearest neighbors refers to identifying the two data points that are closest to a given data point in a dataset. While they are similar concepts, the difference lies in the type of data being analyzed.

4. Can finding the two closest values be applied to non-numerical data?

No, finding the two closest values is typically used for numerical data only. It relies on the concept of distance and comparison between numbers, which is not applicable to non-numerical data. However, a similar method can be used for non-numerical data by identifying the two data points that are most similar to a given data point.

5. How do you handle ties when finding the two closest values?

In the case of a tie, where two or more numbers have the same distance from the given value, you can either choose to include all tied values as the closest values or randomly select one of the tied values. The approach may vary depending on the specific application and the level of precision needed.

Similar threads

  • Programming and Computer Science
Replies
29
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
27
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
2K
  • Calculus
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
14
Views
1K
Back
Top