Problem 51610. Find the nearest value to the given one
You are given
- a non-empty vector of unique integer values vec
- a value val for which you need to find the nearest one.
If two numbers are at the same distance, you need to choose the smallest one.
Example
vec=[17, 7, 11, 10, 12, 4]
val=9
If we sort vec in the ascending order, we have [4, 7, 10, 11, 12, 17] then to the left of number val=9 there is the number 7 and to the right the number 10. Since 10 is closer than 7, the correct answer is 10.
If val=8, the correct answer is 7.
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers30
Suggested Problems
-
Remove all the words that end with "ain"
2174 Solvers
-
The sum of the numbers in the vector
582 Solvers
-
Is this triangle right-angled?
5438 Solvers
-
Change the first and last diagonal element of the identity matrix to zero
146 Solvers
-
Check that number is whole number
3967 Solvers
More from this Author24
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!