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?
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
Y[0](i-1 = -1)? Can you add some small sample input and desired output?