[Numpy-discussion] Simple problem. Is it possible without a loop?

Robert Elsner mlist at re-factory.de
Wed Jun 9 04:31:07 EDT 2010


> Given a certain value delta, I would like to get a subset of x, named
> y, 
> where (y[i+1] - y[i]) >= delta

So in fact the problem is to find y such that

(y[i(k)+n] - y[i(k)]) >= delta
for n <= len(x) - 1 - i
and i(0) = 0, i(k+1) = i(k) + n 

? Well a loop or list comparison seems like a good choice to me. It is
much more obvious at the expense of two LOCs. Did you profile the two
possibilities and are they actually performance-critical?

cheers

Am Mittwoch, den 09.06.2010, 10:14 +0200 schrieb "V. Armando Solé":
> That was my first thought, but that only warrants me to skip one point 
> in x but not more than one.
> 
>  >>> x= numpy.arange(10.)
>  >>> delta = 3
>  >>> print x[(x[1:] - x[:-1]) >= delta]
> []
> 
> instead of the requested [0, 4, 8]
> 
> Armando
> 
> Francesc Alted wrote:
> > A Wednesday 09 June 2010 10:00:50 V. Armando Solé escrigué:
> >   
> >> Well, this seems to be quite close to what I need
> >>
> >> y = numpy.cumsum((x[1:]-x[:-1])/delta).astype(numpy.int)
> >> i1 = numpy.nonzero(y[1:] > y[:-1])
> >> y = numpy.take(x, i1)
> >>     
> >
> > Perhaps this is a bit shorter:
> >
> > y = x[(x[1:] - x[:-1]) >= delta]
> >
> >   
> 
> 
> _______________________________________________
> 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