Given a series of event times v, I can create their interval durations using np.diff(v). Is there a way to have np.diff assume the ser
Given for example:
t = np.array([1.1, 2.0, 4.5, 4.9, 5.2])
We want to compute the consecutive differences in t, including the diff from 0. to the first element in t.
The question gave this way of accomplishing this:
>>> np.diff(np.hstack((0, t)))
And it could be this too:
>>> np.hstack((t[0], np.diff(t)))
But the obscurely-named function ediff1d can do it in one function call:
>>> np.ediff1d(t, to_begin=t[0])
array([ 1.1, 0.9, 2.5, 0.4, 0.3])
Prepending t[0] to the result is the same as computing the difference t[0] - 0., of course. (Assuming t is nonempty).
Timings (not the motivation of the question, but I was curious)
import numpy as np
t = np.random.randn(10000)
%timeit np.diff(np.concatenate(([0], t)))
10000 loops, best of 3: 23.1 µs per loop
%timeit np.diff(np.hstack((0, t)))
10000 loops, best of 3: 31.2 µs per loop
%timeit np.ediff1d(t, to_begin=t[0])
10000 loops, best of 3: 92 µs per loop