Reputation: 153
I have a numpy array X. I need to create another array (say Y) of the same size, which has elements
Y[i] = X[i+1]-X[i-1]
Can I do that without looping over array elements?
Upvotes: 2
Views: 561
Reputation: 420
You could make new arrays with shifted values and then subtract them from one another. Something like this:
import numpy as np
X = np.arange(10)
X1 = np.roll(X,-1) #right shift
X2 = np.roll(X,1) #left shift
Y = X1 - X2
Upvotes: 2