title: Efficient way of computing second min value
body: <p>Given a matrix, it's easy to compute the value and index of the min value:</p>
  
  <pre><code>A = rand(10);
  [value, index] = min(A(:));
  </code></pre>
  
  <p>However I would also like to recover the second min value (idem for max).</p>
  
  <p>I can of course take any of this two approaches:</p>
  
  <ol>
  <li><p>Converting A to a vector and sorting it.</p>
  
  <p>PROS: I can then recover the second, third... n minimum value</p>
  
  <p>CONS: If A is large, sorting is expensive</p></li>
  <li><p>Once the min location of A is located, I can replace this value by a large one (eg: Inf) and then run <code>min</code> again.</p>
  
  <p>PROS: Cheaper than sort</p>
  
  <p>CONS: I must modify my matrix (and save the modified value in an aux variable). Also re-running min is costly on a large matrix.</p></li>
  </ol>
  
  <p>I'm wondering if there is a better solution:</p>
  
  <p>When computing <code>min</code> the algorithm has to keep track of the min value found so far, until a new value has a lower value (then we update the value). 
  If instead we keep track of the last <code>n</code> min values found so far will allow to recover the minimum <code>n</code> values.</p>
  
  <p>I can implement this, but I'm wondering if it's the best approach or if it's already implemented.</p>