Element-wise minimum of multiple vectors in numpy
<p>I know that in numpy I can compute the element-wise minimum of two vectors with</p>

<pre><code>numpy.minimum(v1, v2)
</code></pre>

<p>What if I have a list of vectors of equal dimension, <code>V = [v1, v2, v3, v4]</code> (but a list, not an array)? Taking <code>numpy.minimum(*V)</code> doesn't work. What's the preferred thing to do instead?</p>

<p>Convert to NumPy array and perform <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.min.html" rel="nofollow"><code>ndarray.min</code></a> along the first axis -</p>

<pre><code>np.asarray(V).min(0)
</code></pre>

<p>Or simply use <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.amin.html" rel="nofollow"><code>np.amin</code></a> as under the hoods, it will convert the input to an array before finding the minimum along that axis -</p>

<pre><code>np.amin(V,axis=0)
</code></pre>

<p>Sample run -</p>

<pre><code>In [52]: v1 = [2,5]

In [53]: v2 = [4,5]

In [54]: v3 = [4,4]

In [55]: v4 = [1,4]

In [56]: V = [v1, v2, v3, v4]

In [57]: np.asarray(V).min(0)
Out[57]: array([1, 4])

In [58]: np.amin(V,axis=0)
Out[58]: array([1, 4])
</code></pre>

<p>If you need to final output as a list, append the output with <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tolist.html" rel="nofollow"><code>.tolist()</code></a>.</p>

python numpy