[Numpy-discussion] loop through values in a array and find maximum as looping

josef.pktd at gmail.com josef.pktd at gmail.com
Tue Dec 6 20:36:10 EST 2011


On Tue, Dec 6, 2011 at 7:55 PM, Olivier Delalleau <shish at keba.be> wrote:
> It may not be the most efficient way to do this, but you can do:
> mask = b > a
> a[mask] = b[mask]
>
> -=- Olivier
>
> 2011/12/6 questions anon <questions.anon at gmail.com>
>>
>> I would like to produce an array with the maximum values out of many
>> (10000s) of arrays.
>> I need to loop through many multidimentional arrays and if a value is
>> larger (in the same place as the previous array) then I would like that
>> value to replace it.
>>
>> e.g.
>> a=[1,1,2,2
>> 11,2,2
>> 1,1,2,2]
>> b=[1,1,3,2
>> 2,1,0,0
>> 1,1,2,0]
>>
>> where b>a replace with value in b, so the new a should be :
>>
>> a=[1,1,3,2]
>> 2,1,2,2
>> 1,1,2,2]
>>
>> and then keep looping through many arrays and replace whenever value is
>> larger.
>>
>> I have tried numpy.putmask but that results in
>> TypeError: putmask() argument 1 must be numpy.ndarray, not list
>> Any other ideas? Thanks

if I understand correctly it's a minimum.reduce

numpy

>>> a = np.concatenate((np.arange(5)[::-1], np.arange(5)))*np.ones((4,3,1))
>>> np.minimum.reduce(a, axis=2)
array([[ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.],
       [ 0.,  0.,  0.]])
>>> a.T.shape
(10, 3, 4)

python with iterable

>>> reduce(np.maximum, a.T)
array([[ 4.,  4.,  4.,  4.],
       [ 4.,  4.,  4.,  4.],
       [ 4.,  4.,  4.,  4.]])
>>> reduce(np.minimum, a.T)
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

Josef

>>
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list