Reputation: 1289
I have:
in = array([4, 3, 2, 1,
-3, -2, -1, 0])
How do I subtract the previous element from the next element like this:
out = array([(3-4), (2-3), (1-2),
(-3-1), (-2-(-3)), (-1-(-2)), (0-1)]
out = array([-1,-1,-1,-4,1,1,-1])
Upvotes: 0
Views: 39
Reputation: 13939
You can simply use np.diff()
:
import numpy as np
arr = np.array([4, 3, 2, 1, -3, -2, -1, 0])
print(np.diff(arr)) # [-1 -1 -1 -4 1 1 1]
np.diff()
returns an numpy array you just described: the differences between two adjacent elements.
Upvotes: 2