I would like to roll a 2D numpy array, each row being rolled by an amount defined in a 1D array. For example, I have
A=np.array([[1,2,3],
[4,5,6],
[7,8,9]])
r=[1,2,2]
And I wish to perform the following task:
C=np.copy(A)
for i in range(3):
C[i]=np.roll(C[i],r[i])
print(C)
[[3 1 2]
[5 6 4]
[8 9 7]]
Apparently, numpy roll function supports arrays as input. But the way it works is puzzling, and I don't get what I think I should get:
B=np.roll(A,r,1)
print(B)
[[2 3 1]
[5 6 4]
[8 9 7]]
Here, all the rows have been shifted by the same amount (which from my experiments seems to be the sum of the elements of my 1D array). What did I do wrong here? numpy.roll is much faster than a for loop, so I'd like to use it if possible, but I can't get it to output what I want.