I have a 2d numpy array and I want to roll each row in an incremental fashion. I am using np.roll
in a for
loop to do so. But since I am calling this thousands of times, my code is really slow. Can you please help me out on how to make it faster.
My input looks like
array([[4,1],
[0,2]])
and my output looks like
array([[4,1],
[2,0]])
Here the zeroth row [4,1]
was shifted by 0, and the first row [0,2]
was shifted by 1. Similarly the second row will be shifted by 2 and so on.
EDIT
temp = np.zeros([dd,dd])
for i in range(min(t + 1, dd)):
temp[i,:] = np.roll(y[i,:], i, axis=0)