Feed on
Posts
Comments

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):

>> [min_difference, 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!

25 Responses to “in matlab, find the array position of an entry closest to some arbitrary value”

  1. on 01 Feb 2007 at 1:01 pm Cruchon

    Something like
    [rte,index]=min(abs(X-x0)); where x0 is the target and X is the array you want to search in works. The answer is index.

  2. on 01 Feb 2007 at 1:08 pm Lawrence David

    i believe that’s exactly what i wrote above.

  3. on 29 May 2007 at 10:28 am Roman

    Thanks guys!

  4. on 20 Jul 2007 at 4:00 pm Hilikus

    ??? that returns the difference between the closest and the target vale not its INDEX

  5. on 20 Jul 2007 at 4:13 pm lawrence

    hilikus:

    the second argument returned is the index; the first argument returned is the difference.

  6. on 20 Jul 2007 at 4:21 pm Hilikus

    Bah, nevermind, i didnt see the double output

  7. on 15 Sep 2007 at 10:18 am D

    >> [trash array_position] = min(abs(a - k))

    probably should have a “,” or odd things may happen.

    [trash, array_position] = min(abs(a - k))

    I wasted a lot of time recently and that simple problem caused it.

    D

  8. on 29 Feb 2008 at 2:58 pm jess

    I love you for this! *mwah*

  9. on 05 Mar 2008 at 12:44 pm huzzzaa

    thanks guys!!!!!!!!! saved me hours developing my own algorithm!

  10. on 28 May 2008 at 12:43 pm Chan

    Man, you guys really rock! Thanks a lot, saved me hours. Keep up the good work!!

  11. on 27 Aug 2008 at 12:01 pm marchessoux

    Thanks a lot, exacty what I needed, so economy of many time ;=)
    Cédric

  12. on 23 Sep 2008 at 11:44 am dave

    can you do something similar but for several k’s at a time?

  13. on 04 Dec 2008 at 2:23 am MoneyG

    Is there a similar way to get position information out of a matrix.

    I tried something like,
    [row,col]=min(min(matrix))
    but that returns only the final row that the element is in, not its exact location.

  14. [...] to see the wood for the trees”. I guess it is also know to non-German speakers? I just found this very trivial, banal and easy Matlab command line, which makes my equivalent lines look very stupid and [...]

  15. on 17 Feb 2009 at 4:02 am Tom

    Nice! Saved me some time!

  16. on 11 Mar 2009 at 5:29 am Sameer

    How Matlab search the object or variable name in simulink model.
    mean Functionality of Edit->Find…(Ctrl+F).

  17. on 11 May 2009 at 12:03 pm cato

    but what if you want to be sure you at the value closest to this value right/left side?

    for instance, a cumulative probability vector and you want to find the index for the value that is the first above 0.5?

  18. on 22 Aug 2009 at 2:59 pm efrain

    greato!
    thanks a lot! :D

  19. on 27 Aug 2009 at 1:33 pm sharkey

    Thanks for post this! You just made my life easier.

  20. on 27 Aug 2009 at 1:35 pm sharkey

    apparently, the space this took in my brain was previously occupied by my knowledge of the English language… sorry.

    Thanks for posting this. You just made my life easier.

  21. on 21 Nov 2009 at 6:53 pm penguin

    you could use something like this for multiple ‘k’s (’range’ == ‘a’, ‘vector’ is a set of ‘k’s):

    function inds = findClosest(range, vector)
    % ‘flatten’ everything into row vectors
    origshape = size(vector);
    sz_r = size(range);
    numTargets = sz_r(1) * sz_r(2);
    numInputs = origshape(1) * origshape(2);

    range = reshape(range, [1, numTargets]);
    vector = reshape(vector, [1, numInputs]);

    % find the difference between the target values and the actual values
    % ‘range’ is replicated across columns,
    % ‘vector’ is replicated across rows
    diff = repmat(range’, 1, numInputs);
    diff = diff - repmat(vector, numTargets, 1);
    diff = abs(diff);

    [vals, inds] = min(diff);
    inds = reshape(inds, origshape);
    end

  22. on 09 Mar 2010 at 5:11 am Max

    Much better than my routine, which always
    gets confused by the extrema of the array.
    And faster too, love it.

  23. [...] why multiple output arguments in Matlab were a poor design decision. But here is a start. In an unsuccessful attempt to find a code that’s less than O(N*M) in storage for a problem that in a…, I saw that someone on Yahoo! Answers wants to know, Is there a similar way to get position [...]

  24. on 10 Aug 2010 at 3:04 pm Jay Mung

    Hey Lawrence. Columbia BME ‘04 alum here. I googled for your little trick here and was pleasantly surprised to find your blog. Keep up the good work.

    Jay

  25. on 23 Aug 2010 at 11:54 am Alexander

    Lawrence, I really enjoy your blog, I have found useful answers to many questions. This one was not the exception. Thanks a lot and keep up the great work.

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

More blogs about http://desk.stinkpot.org:8080/tricks.