Use `np.diff` but assume the input starts with an extra zero

后端 未结 2 714
孤城傲影
孤城傲影 2021-01-18 01:45

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

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-18 02:33

    As of 2019, np.diff has the arguments prepend and append that can add a certain value to the array before differentiation. See the docs

    This would append the first value to the array, hence the diff operation would return something of len(t) that starts with 0.

    >>> t = np.array([1.1, 2.0, 4.5, 4.9, 5.2])
    >>> np.diff(t, prepend=t[0])
    array([0. , 0.9, 2.5, 0.4, 0.3])
    

    The prepend argument can take other values.

提交回复
热议问题