Numpy rot90 function implemented in the rotation matrix

From the official full NumPy found rot90 function syntax is as follows:

rot90(m, k=1, axes=(0, 1)

m is to rotate the array (matrix), k is the number of rotations, the default rotation once it clockwise or counter-clockwise it? Positive numbers indicate counterclockwise, k is a negative value while the array is rotating is clockwise. axes is the plane defined by the axis, the axis perpendicular to the plane of rotation, the axis must be different, for rotating the three-dimensional matrix.

import numpy as np
mat = np.array([[1,3,5],
                [2,4,6],
                [7,8,9]
                ])
print mat, "# orignal"
mat90 = np.rot90(mat, 1)
print mat90, "# rorate 90 <left> anti-clockwise"
mat90 = np.rot90(mat, -1)
print mat90, "# rorate 90 <right> clockwise"
mat180 = np.rot90(mat, 2)
print mat180, "# rorate 180 <left> anti-clockwise"
mat270 = np.rot90(mat, 3)
print mat270, "# rorate 270 <left> anti-clockwise"

Results of the:

[[1 3 5]
 [2 4 6]
 [7 8 9]] # orignal
[[5 6 9]
 [3 4 8]
 [1 2 7]] # rorate 90 <left> anti-clockwise
[[7 2 1]
 [8 4 3]
 [9 6 5]] # rorate 90 <right> clockwise
[[9 8 7]
 [6 4 2]
 [5 3 1]] # rorate 180 <left> anti-clockwise
[[7 2 1]
 [8 4 3]
 [9 6 5]] # rorate 270 <left> anti-clockwise

Rotation about the Z axis of a three dimensional matrix:

import  numpy as np

if __name__ == '__main__':
    weights = np.array(
        [[[-1, 1, 0],
          [0, 1, 0],
          [0, 1, 1]],
         [[-1, -1, 0],
          [0, 0, 0],
          [0, -1, 0]],
         [[0, 0, -1],
          [0, 1, 0],
          [ 1, -1, -1]]] = dtype np.float64)
    flipped_weights = np.rot90(weights, 2 , (1,2))
    print(flipped_weights)

Results of the:

[[[ 1.  1.  0.]
  [ 0.  1.  0.]
  [ 0.  1. -1.]]
 [[ 0. -1.  0.]
  [ 0.  0.  0.]
  [ 0. -1. -1.]]
 [[-1. -1.  1.]
  [ 0.  1.  0.]
  [-1.  0.  0.]]]

 

reference:

http://liao.cpython.org/numpy13/

https://blog.csdn.net/weixin_39506322/article/details/89463286

Guess you like

Origin www.cnblogs.com/ratels/p/12310493.html