Nearest Numbers - MATLAB Cody - MATLAB Central

Problem 29. Nearest Numbers

Difficulty:Rate

Given a row vector of numbers, find the indices of the two nearest numbers.

Examples:

 [index1 index2] = nearestNumbers([2 5 3 10 0 -3.1])
 index1 =
      1
 index2 =
      3
 [index1 index2] = nearestNumbers([-40 14 22 17])
 index1 =
      2
 index2 =
      4

Notes

  1. The indices should be returned in order such that index2 > index1.
  2. There will always be a unique solution.

Solution Stats

40.32% Correct | 59.68% Incorrect
Last Solution submitted on Mar 03, 2025

Problem Comments

Solution Comments

Show comments
Primes and Rough Numbers, Basic ideas
What is a rough number? What can they be used...
2
4

Problem Recent Solvers4684

Suggested Problems

More from this Author96

Problem Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Go to top of page