如何使用C或Python将矩阵逆时针旋转一个元素n次?

2024-05-29 00:01:08 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我给出一个矩阵

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矩阵也旋转一个元素。已作出尝试,但未成功。我没有把失败的代码。此外,将此作为问题发布的想法是,这里的专家将提供接近最优的解决方案/想法。这些想法是针对python语言提出的,只是为了澄清

[矩阵旋转可视化][1] [1]: http://i.stack.imgur.com/WXOd3.png


Tags: 代码com语言http元素pngstack可视化
2条回答

我找到了一个顺时针旋转的代码。似乎很容易交换左右变量并获取所需内容

# 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/

不需要把事情复杂化

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]]

相关问题 更多 >

    热门问题