如何使用C或Python在逆时针方向旋转一个元素?

时间:2016-06-15 15:23:10

标签: python matrix rotation

如果我给出一个矩阵

a=[[1,2,3,4],
   [5,6,7,8],
   [9,10,11,12],
   [13,14,15,16]]

4 * 4矩阵。我想逆时针旋转这个矩阵一个元素。结果应该是

a=[[2,3,4,8],
  [1,7,11,12],
  [5,6,10,16],
  [9,13,14,15]]

外部元素旋转一个元素,内部2 * 2矩阵也旋转一个元素。

Matrix Rotation Visualization

2 个答案:

答案 0 :(得分:1)

无需复杂化。

a = [[1,2,3,4],
     [5,6,7,8],
     [9,10,11,12],
     [13,14,15,16]]

a = [[a[0][1],a[0][2],a[0][3],a[1][3]],
     [a[0][0],a[1][2],a[2][2],a[2][3]],
     [a[1][0],a[1][1],a[2][1],a[3][3]],
     [a[2][0],a[3][0],a[3][1],a[3][2]]]

print(a)

输出:

[[2, 3, 4, 8], [1, 7, 11, 12], [5, 6, 10, 16], [9, 13, 14, 15]]

但是,如果你必须,一般的解决方案(几乎就地):

a = [[1,2,3,4],
     [5,6,7,8],
     [9,10,11,12],
     [13,14,15,16]]

g = [[1,2,3,4,5,6],
     [7,8,9,10,11,12],
     [13,14,15,16,17,18],
     [19,20,21,22,23,24],
     [25,26,27,28,29,30],
     [31,32,33,34,35,36]]

def rotate_ring(matrix, offset):
    dim = len(matrix[0])
    last_element = matrix[offset][offset]
    for j in range(1+offset, dim-offset):
        matrix[offset][j-1] = matrix[offset][j]
    matrix[offset][dim-1-offset] = matrix[1+offset][dim-1-offset]
    for i in range(1+offset, dim-offset):
        matrix[i-1][dim-1-offset] = matrix[i][dim-1-offset]
    matrix[dim-1-offset][dim-1-offset] = matrix[dim-1-offset][dim-2-offset]
    for j in range(1+offset, dim-offset):
        matrix[dim-1-offset][dim-j] = matrix[dim-1-offset][dim-j-1]
    matrix[dim-1-offset][offset] = matrix[dim-2-offset][offset]
    for i in range(1+offset, dim-offset):
        matrix[dim-i][offset] = matrix[dim-i-1][offset]
    matrix[1+offset][offset] = last_element

def rotate_matrix(matrix):
    dim = len(matrix[0])
    for offset in range(0, int(dim/2)):
        rotate_ring(matrix, offset)

rotate_matrix(a)
rotate_matrix(g)

print(a)
print(g)

输出:

[[2, 3, 4, 8], [1, 7, 11, 12], [5, 6, 10, 16], [9, 13, 14, 15]]
[[2, 3, 4, 5, 6, 12], [1, 9, 10, 11, 17, 18], [7, 8, 16, 22, 23, 24], [13, 14, 15, 21, 29, 30], [19, 20, 26, 27, 28, 36], [25, 31, 32, 33, 34, 35]]

答案 1 :(得分:0)

我找到了一个顺时针旋转的代码。似乎很容易交换左右变量,并采取你想要的

# Function to rotate a matrix
def rotateMatrix(mat):

    if not len(mat):
        return

    """
        top : starting row index
        bottom : ending row index
        left : starting column index
        right : ending column index
    """

    top = 0
    bottom = len(mat)-1

    left = 0
    right = len(mat[0])-1

    while left < right and top < bottom:

        # Store the first element of next row,
        # this element will replace first element of
        # current row
        prev = mat[top+1][left]

        # Move elements of top row one step right
        for i in range(left, right+1):
            curr = mat[top][i]
            mat[top][i] = prev
            prev = curr

        top += 1

        # Move elements of rightmost column one step downwards
        for i in range(top, bottom+1):
            curr = mat[i][right]
            mat[i][right] = prev
            prev = curr

        right -= 1

        # Move elements of bottom row one step left
        for i in range(right, left-1, -1):
            curr = mat[bottom][i]
            mat[bottom][i] = prev
            prev = curr

        bottom -= 1

        # Move elements of leftmost column one step upwards
        for i in range(bottom, top-1, -1):
            curr = mat[i][left]
            mat[i][left] = prev
            prev = curr

        left += 1

    return mat

它的Python。导入numpy,然后制作表格:

m = np.array([[1,2,3,4],[5,6,7,8,9],[10,11,12,13,14]], int)

旋转并调用函数rotateMatrix():

m1=rotateMatrix(mat)
print m1

希望有所帮助

代码来源:http://www.geeksforgeeks.org/rotate-matrix-elements/