numpy.where

Carl Banks pavlovevidence at gmail.com
Thu Apr 9 13:33:37 EDT 2009


On Apr 9, 2:58 am, Neil Crighton <neilcrigh... at gmail.com> wrote:
> Carl Banks <pavlovevidence <at> gmail.com> writes:
>
> > > >>> condition = (min_time <= time) & (time <= max_time)
> > > >>> new_time = time[condition]
> > > >>> new_energy = energy[condition]
>
> > Won't work: condition is an array of ones and zeros, but you need to
> > index the arrays with indices.  So, add a call to nonzero to get the
> > indices of the elements.
>
> > elements = nonzero(logical_and(min_time<=time,max_time>=time))
>
> > new_time = time[elements]
> > new_energy = energy[elements]
>
> It will work - try it :)
>
> You can index numpy arrays with either an array of indices (dtype integer)
> ,or a boolean array (dtype bool). In the case above condition is a boolean
> array. In general using a boolean array is faster, because you skip the call
> to nonzero().

New to me.  Thanks for the tip.


Carl Banks



More information about the Python-list mailing list