I have a 4x4 matrix like this:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
I want to shift each row left (left circular shift), by the amount of the row index. I.e. row 0 stays as is, row 1 shifts left 1, row 2 shifts left 2, etc.
So we get this:
1 2 3 4
6 7 8 5
11 12 9 10
16 13 14 15
The fastest way I've come up with to do this in Python is the following:
import numpy as np
def ShiftRows(x):
x[1:] = [np.append(x[i][i:], x[i][:i]) for i in range(1, 4)]
return x
I need to run this function on thousands of 4x4 matrices like this, so speed is important (to the extent possible in Python). I'm not concerned about using other modules such as numpy, I'm only concerned with speed.
Any help would really be appreciated!
Thank you!