Always use numpy
for matrix operations. I’ll assume an m*n
numpy array arr
. I first did a transpose using the np.transpose
function and then I flipped it using the np.fliplr
function.
output = np.fliplr(np.transpose(arr))
As mentioned in the comments, there is no way to do an in-place replace without a temporary variable for rectangular matrices. It’s better to simulate the behavior (if storage is your concern) using a function like this,
def clockwise(matrix, row, column):
return matrix[-(column + 1)][row]
2
solved Inplace rotation of a matrix