probably a stupid question: MatLab equivalent of "diff" ?
Carl Banks
pavlovevidence at gmail.com
Fri Dec 29 23:17:24 EST 2006
Stef Mientki wrote:
> Does anyone know the equivalent of the MatLab "diff" function.
> The "diff" functions calculates the difference between 2 succeeding
> elements of an array.
> I need to detect (fast) the falling edge of a binary signal.
Using numpy (or predecessors), you can do this easily with slicing:
a[1:] - a[:-1]
Slicing shares data, so the slices above don't create new arrays, but
new views into the old array, so it avoids that overhead.
For Python lists you'd have to use a list comprehension or some such
thing like that. For example:
[ x-y for (x,y) in zip(a[1:],a[:-1]) ]
Or, to avoid creating two new arrays, use iterators:
import itertools
[ x-y for (x,y) in
itertools.izip(itertools.islice(a,0,-1),itertools.islice(a,1)) ]
Carl
More information about the Python-list
mailing list