Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit 2</strong>: As Eitan pointed the first part of the answer doesn't address the question of finding the smallest m-th value but regarding the m-th element after the min value. The rest of the answer remains... +1 for Eitan's sharpness. <strike> While <code>sort</code> is probably very efficient to begin with, you can try to see whether a <code>find</code> will be better. For example:</p> <pre><code>id=find(X&gt;min(X),m,'first'); id(end) % is the index of the smallest m-th element in X </code></pre> <p>the function <code>find</code> has added functionality that lets you find the 'first' or 'last' elements that meet some criterion. For example, if you want to find the first <code>n</code> elements in array <code>X</code> less than a value <code>y</code>, use <code>find(X&lt;y,n,'first')</code></p> <p>This operation stops as soon as the first element meeting the condition is encountered, which can result in significant time savings if the array is large and the value you find happens to be far from the end. </strike></p> <p>I'd also like to recap what @woodchips said already in <a href="https://stackoverflow.com/questions/7019975/how-to-speed-up-sort-function-in-matlab">this SO discussion</a> that is somewhat relevant to your question: </p> <p>The best way to speed up basic built-in algorithms such as sort is to get a faster hardware. It will speed everything else up too. MATLAB is already doing that in an efficient manner, using an optimized code internally. Saying this, maybe a GPU add-on can improve this too...</p> <p><strong>Edit:</strong> For what it's worth, adding to Muster's comment, there is a FEX file called <a href="http://www.mathworks.com/matlabcentral/fileexchange/29453-nthelement" rel="nofollow noreferrer">nth_element</a> that is a MEX wrap of C++ that will get a solution in <code>O(n)</code> time for what you need. (similar to what @DDD pointed to) </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload