I am trying to implement a function that will shift a particular element in a numpy 2d array a desired number of spaces in a given direction. Empty spaces should be filled with 0's. This function will take as input a numpy array, the x and y coordinates, a desired direction, and the number of spaces to shift.
For example, where shift is the hypothetical function:
arr = np.array([1, 1, 1], [1, 1, 1], [1, 1, 1])
arr out: [[1, 1, 1],
[1, 1, 1],
[1, 1, 0]]
shift(arr, 0, 0, 'right', 2)
arr out: [[0, 0, 1],
[1, 1, 1],
[1, 1, 0]]
shift(arr, 0, 2, 'down', 1)
arr out: [[0, 0, 0],
[1, 1, 1],
[1, 1, 1]]
I have found that I can achieve the desired shifting of all elements of either a row or column along that row or column with numpy's roll function. However, this approach simply cycles elements back to the beginning of the same row or column and does not fill empty spaces with 0's. For example:
arr[:, 0] = np.roll(arr[:, 0], 1)
arr out: [[1, 0, 0],
[0, 1, 1],
[1, 1, 1]]
Any assistance is very much appreciated.
edit: The x and y coordinates are the coordinates of the element to be shifted within the 2d array. The the rest of the elements within the same row or column are then shifted with respect to that element in the desired direction. For example shift(arr, 2, 2, 'down', 1) shifts the elements in the column with respect to the element at (2, 2) down by 1. All input values may be assumed to be valid at all times.
edit: This problem differs from the one linked in that elements are shifted with respect to the element at the coordinates provided, and this shifting occurs in a nested array. Furthermore, this solution does not allow for shifting of elements either up or down within the same column.