[Numpy-discussion] argmin & min on ndarrays

Pierre GM pgmdevlist at gmail.com
Tue Mar 4 16:31:51 EST 2008


All,
Let a & b be two ndarrays of the same shape. I'm trying to find the elements 
of b that correspond to the minima of a along an arbitrary axis.
The problem is trivial when axis=None or when a.ndim=2, but I'm getting 
confused with higher dimensions: I came to the following solution that looks 
rather ugly, and I'd need some ideas to simplify it

>>>a=numpy.arange(24).reshape(2,3,4)
>>>axis=-1
>>>b = numpy.rollaxis(a,axis,0)[a.argmin(axis)][tuple([0]*(a.ndim-1))]
>>>numpy.all(b, a.min(axis))
True

Thanks a lot in advance for any suggestions.





More information about the NumPy-Discussion mailing list