Find nth least value in multiple numpy arrays
<p>In numpy, I can find which 2D array is the least of all 3 2D arrays as follows:</p>

<pre><code>mat_a = np.random.random((5, 5))
mat_b = np.random.random((5, 5))
mat_c = np.random.random((5, 5))
bigmat = np.stack((mat_a, mat_b, mat_c)) # this is a 3, 5, 5 array
minima = np.argmin(bigmat, axis=0) # contains a 5x5 array of 0,1,2 for a,b,c respectively
</code></pre>

<p>How do I extend this, so that it works to find 2nd least of all 3 2D arrays?</p>

<p>-- EDIT:</p>

<p>Expected output is a 5 x 5 numpy array, where each element is represents which of the 3 arrays (mat_a, mat_b, mat_c) is the 2nd least value in bigmat. </p>

<p>So, structure will be same as <code>minima</code>, except <code>minima</code> can only show which array is least.</p>

<p>I'll bet <code>np.argsort()</code> is the most efficient way.</p>

<p>FYI, here's an interesting thing you can do with array indexing that will also solve your problem (though less efficiently):</p>

<pre><code>max_value = np.max(bigmat)
maskmat = np.array([minima == i for i in xrange(bigmat.shape[0])])
bigmat[maskmat] = max_value  # assign each minimum to the array-wide maximum
minima2 = np.argmin(bigmat, axis=0)
</code></pre>

python numpy