I am trying to re-center a set of image-like data where I know the appropriate offset in the x- and y- dimensions. Similarly to this question I want to do something like a "roll" but instead of assuming periodic boundary conditions I want to fill in the "empty" positions in the array with zeros.
My question is more general than the one that I have linked because I would like to shift an array in arbitrary directions. In the accepted solution, one must always know what direction the image is being rolled, in order to cut off the appropriate edge. In particular, here is the accepted solution which involves shifting the array x by 1.
import numpy as np
x = np.array([[1, 2, 3],[4, 5, 6]])
print np.pad(x,((0,0),(1,0)), mode='constant')[:, :-1]
However to do the reverse operation, one would have to hard code the following change:
import numpy as np
x = np.array([[1, 2, 3],[4, 5, 6]])
print np.pad(x,((0,0),(0,1)), mode='constant')[:, 1:]
// changed argument of pad
// and array indices
Is there a simple way to implement both at once?