Send As SMS

3/4/2006

find the array position of an entry closest to some arbitrary value

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.


1 Comments:

Anonymous said...

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

Mike

5/02/2006 03:35:04 PM  

Post a Comment

Links to this post:

Create a Link

<< Home

My blog has moved! Redirecting...

You should be automatically redirected. If not, visit http://stinkpot.afraid.org:8080/tricks/ and update your bookmarks.