I had this problem assigned to me in class. I'm still learning MATLAB, so I couldn't figure out how to solve this one using indices. The problem is: Given a row vector of numbers, find the indices of the two closest numbers. For instance:
[index1 index2]=nearestNumbers([2 6 3 13 0 -16.1])
This would output:
index1 = 1
index2 = 3
Since the numbers 2 and 3 in the vector are closer to each other than
any other pair of numbers
I'm guessing I need to use the find
function here (somewhere along the lines of y = find(min())
) but I'm just not sure how to translate that into a coherent line of code. I tried using the find function I mentioned, but it just gives me a vector row of 0's. Your help would really be appreciated!