I’d like to roll a 2D numpy array, except that I’d like to fill the ends with zeros rather than roll the data as if it were periodic.
The following
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6]])
np.roll(x, 1, axis=1)
returns
array([[3, 1, 2], [6, 4, 5]])
but what I would prefer is
array([[0, 1, 2], [0, 4, 5]])
I don’t think that you are going to find an easier way to do this that is built-in. The touch-up seems quite simple to me:
If you want this to be more direct then maybe you could copy the roll function to a new function and change it to do what you want. The roll() function is in the
site-packages\core\numeric.pyfile.