Feed on

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.

Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

One Response to “find the array position of an entry closest to some arbitrary value”

  1. on 02 May 2006 at 3:35 pm Anonymous

    I thought there would be a cleaner way than using a for loop. Thanks that was perfect.


Did I get this wrong? Let me know!

Trackback URI | Comments RSS