If you can describe the elements you want to rotate using advanced indexing , then you can rotate using something like the following (if your array is called arr ):
arr[rs:re,cs:ce] = np.rot90(np.copy(arr[rs:re,cs:ce]))
Here rs , re , cs and ce mean the beginning of the line and the end of the slice line and the beginning of the column and the end of the column, respectively.
Here is an example of why you need to call np.copy (at least in numpy 1.3.0):
>>> import numpy as np >>> m = np.array([[i]*4 for i in range(4)]) >>> m array([[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]) >>> m[1:3,1:3] = np.rot90(m[1:3,1:3])
source share