[Numpy-discussion] Set values of a matrix within a specified range to zero

Friedrich Romstedt friedrichromstedt at gmail.com
Tue Mar 30 17:16:54 EDT 2010


2010/3/30 Ryan May <rmay31 at gmail.com>:
> On Tue, Mar 30, 2010 at 11:12 AM, Alan G Isaac <aisaac at american.edu> wrote:
>> On 3/30/2010 12:56 PM, Sean Mulcahy wrote:
>>> 512x512 arrays.  I would like to set elements of the array whose value fall within a specified range to zero (eg 23<  x<  45).
>>
>> x[(23<x)*(x<45)]=0
>
> Or a version that seems a bit more obvious (doing a multiply between
> boolean arrays to get an AND operator seems a tad odd):
>
> x[(23<x) & (x<45)] = 0

We recently found out that it executes faster using:

x *= ((x <= 23) | (x >= 45))  .

Friedrich



More information about the NumPy-Discussion mailing list