I came up with this question while trying to apply Cesar Cipher to a matrix with different offset values ββfor each row, i.e. taking into account the matrixX
array([[1, 0, 8],
[5, 1, 4],
[2, 1, 1]])
with shift values S = array([0, 1, 1])
, the output should be
array([[1, 0, 8],
[1, 4, 5],
[1, 1, 2]])
This is easy to implement with the following code:
Y = []
for i in range(X.shape[0]):
if (S[i] > 0):
Y.append( X[i,S[i]::].tolist() + X[i,:S[i]:].tolist() )
else:
Y.append(X[i,:].tolist())
Y = np.array(Y)
This is a left-right shift. I wonder how to do this in a more efficient way using numpy arrays?
Update: This example applies the transition to the columns of the matrix. Suppose we have a 3D array
array([[[8, 1, 8],
[8, 6, 2],
[5, 3, 7]],
[[4, 1, 0],
[5, 9, 5],
[5, 1, 7]],
[[9, 8, 6],
[5, 1, 0],
[5, 5, 4]]])
Then a cyclic shift to the right S = array([0, 0, 1])
along the columns leads to
array([[[8, 1, 7],
[8, 6, 8],
[5, 3, 2]],
[[4, 1, 7],
[5, 9, 0],
[5, 1, 5]],
[[9, 8, 4],
[5, 1, 6],
[5, 5, 0]]])