find the array position of an entry closest to some arbitrary value
March 4th, 2006 by Lawrence David
a friend asked:
matlab question: i have an array of data. how do i get matlab to find the position of an entry closest to some arbitrary value?
for instance, i would have
a= [1; 4; 6; 8; 12]
i want to search for 9 and have matlab spit back 4, the entry in a closest to 9.
here’s one way to solve that problem (assuming that variable k=9):
>> [trash array_position] = min(abs(a – k))
wheree array_position is the desired index value.
I thought there would be a cleaner way than using a for loop. Thanks that was perfect.
Mike