[SciPy-user] probably a stupid question: MatLab equivalent of "diff" ?

Lev Givon lev at columbia.edu
Fri Dec 29 10:41:33 EST 2006


Received from Otto Tronarp on Fri, Dec 29, 2006 at 11:26:06AM EST:
> Quoting Stef Mientki <s.mientki at ru.nl>:
> 
> > Does anyone know the equivalent of the MatLab "diff" function.
> > The "diff" functions calculates the difference between 2 succeeding
> > elements of an array.
> 
> Then it is the numpy.diff function you'r looking for.
> 
> In [19]:import numpy
> In [20]:numpy.diff([1,2,4,1])
> Out[20]:array([ 1,  2, -3])
> 
> Otto

Moreover, if you want to ignore the rising edges, you can use
something like this:

a = <array of some integers>
da = diff(a)
where(da>=0,zeros(shape(da),dtype=int),-ones(shape(da),dtype=int)) 

							L.G.



More information about the SciPy-User mailing list