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

Friedrich Romstedt friedrichromstedt@gmail....
Tue Mar 30 16:51:30 CDT 2010


2010/3/30 Ryan May <rmay31@gmail.com>:
> On Tue, Mar 30, 2010 at 3:16 PM, Friedrich Romstedt
> <friedrichromstedt@gmail.com> wrote:
>> We recently found out that it executes faster using:
>>
>> x *= ((x <= 23) | (x >= 45))  .
>
> Interesting. In an ideal world, I'd love to see why exactly that is,
> because I don't think multiplication should be faster than a boolean
> op.  If you need speed, then by all means go for it.  But if you don't
> need speed I'd use the & since that will be more obvious to the person
> who ends up reading your code later and has to spend time decoding
> what that line does.

Hmm, I'm not familiar with numpy's internals, but I guess, it is
because numpy doesn't have to evaluate the indexing boolean array?
When the indexing array is applied to x via x[...] = 0?

Robert's reply just came in when writing this.

Friedrich


More information about the NumPy-Discussion mailing list