[Numpy-discussion] Simple problem. Is it possible without a loop?
Bruce Southey
bsouthey@gmail....
Wed Jun 9 09:24:23 CDT 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@scipy.org <http://mail.scipy.org/mailman/listinfo/numpy-discussion>
> />/ http://mail.scipy.org/mailman/listinfo/numpy-discussion/
/
Playing around with range/arange can be misleading as
>> x[x%4==0]
array([ 0., 4., 8.])
I don't know you really want because your first code
>>> x= numpy.arange(10.)
>>> delta=3
>>> y=[x[0]]
>>> for value in x:
... if (y[-1] -value) < delta:
... y.append(value)
...
>>> y
[0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
Which is not [0, 4, 8].
Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100609/1bab6747/attachment.html
More information about the NumPy-Discussion
mailing list