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

Stef Mientki s.mientki at ru.nl
Fri Dec 29 12:22:57 CST 2006

hi Tom,

Tom Denniston wrote:
> It would help if you could give a concrete example with a small set of
> data as below to demonstrate the problem:
 >>> # the orginal array
 >>> a = array([True,True,False,False,True,True])

 >>> B = numpy.diff(a)
 >>> B
array([False, True, False, True, False], dtype=bool)
 >>> # now elements B[1] and B [3] are true, so both edges

 >>> # that doesn't seem to be unlogical, if we try to subtract booleans
 >>> True - False
 >>> False - True

And now I guess that both -1 and +1 are translated into True,
and can't be distinguished anymore :-(

 >>> # and now also this doesn't work: again 2 edges
 >>> a[1:] - a[:-1]
array([False, True, False, True, False], dtype=bool)
 >>> a[1:] - a[:-1]  > 0
array([False, True, False, True, False], dtype=bool)

 >>> # So the first thing that worked:
 >>> a[1:] &  ~(a[:-1])
array([False, False, False, True, False], dtype=bool)

And that was (besides the history element) the solution I posted in the 
previous mail.
Due to some typing error, I thought my editor didn't except the "&" and 
"~ ",
and therefore I used the longer logical_and, logical_not.

Now I can live quiet well with this,
but as Newbie I want to know that's the correct way,
or are their better ways ?


More information about the SciPy-user mailing list