简体   繁体   English

如何将矩阵(嵌套列表)逆时针旋转90度

[英]How to rotate a matrix (nested list) counter clockwise by 90 degrees

I'm trying to rotate a matrix counter clockwise by 90 degrees. 我试图将矩阵逆时针旋转90度。 For example, if: 例如,如果:

m = [[1,2,3],
     [2,3,3],
     [5,4,3]]

then the result should be 那么结果应该是

m = [[3,3,3],
     [2,3,4],
     [1,2,5]]

So far, I found: 到目前为止,我发现:

rez = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
    for row in rez:
        print(row)

This gives me 这给了我

[1, 2, 5]
[2, 3, 4]
[3, 3, 3]

This is close, but the rows would need to be reverses. 这很接近,但行需要反转。 Does anyone know a simple way to rotate this matrix counter clockwise by 90 degrees? 有没有人知道一种简单的方法将这个矩阵逆时针旋转90度?

You could do the following: 您可以执行以下操作:

m = [[1, 2, 3],
     [2, 3, 3],
     [5, 4, 3]]

result = list(map(list, zip(*m)))[::-1]

print(result)

Output 产量

[[3, 3, 3],
 [2, 3, 4],
 [1, 2, 5]]

With map(list, zip(*m)) you create an iterable of the columns, and with the expression list(...)[::-1] you convert that iterable into a list and reverse it. 使用map(list, zip(*m))您可以创建列的可迭代,并使用表达式list(...)[::-1]将该iterable转换为列表并将其反转。

What you here basically do is map a matrix A to a matrix B such that: 你在这里基本上做的是将矩阵A映射到矩阵B,这样:

B ij =A ji B ij = A ji

In case you rotate elements, that means that if you rotate an n×m -matrix, then that means that: 如果你旋转元素,这意味着如果你旋转一个n×m矩阵,那意味着:

B ij =A j ni B ij = A j ni

So we can calculate this as: 所以我们可以将其计算为:

rez = [[m[j][ni] for j in range(len(m))] for ni in range(len(m[0])-1, -1, -1)]

which is thus the transpose, but than "reversed". 这是转置,但比“逆转”。 Using indices is however typically not how you do such processing in Python, since now it works only for items that are subscriptable, so I advice you to look for a more elegant solution. 然而,使用索引通常不是如何在Python中进行此类处理,因为现在它仅适用于可订阅的项目,因此我建议您寻找更优雅的解决方案。

But that being said, numpy offers a numpy.rot90 function to rotate matrices: 但话虽如此,numpy提供了一个numpy.rot90函数来旋转矩阵:

>>> np.rot90(m)
array([[3, 3, 3],
       [2, 3, 4],
       [1, 2, 5]])

Other option is to use scipy.ndimage.rotate 其他选项是使用scipy.ndimage.rotate

Rotate an array. 旋转一个数组。

The array is rotated in the plane defined by the two axes given by the axes parameter using spline interpolation of the requested order. 使用所请求顺序的样条插值,在由轴参数给出的两个轴定义的平面中旋转阵列。

import numpy as np
from  scipy import ndimage

m = np.matrix([[1,2,3],
               [2,3,3],
               [5,4,3]])

ndimage.rotate(m, 90.0) #angle as float.
 Out: array([[3, 3, 3], [2, 3, 4], [1, 2, 5]]) 

Same result you can get by using the zip() function to transpose rows and columns of a 5.1.4. 通过使用zip()函数转置5.1.4的行和列,可以获得相同的结果 Nested List then reverse the nested list with [::-1] + put in a np.matrix : 嵌套列表然后使用[::-1] +放入np.matrix中反转嵌套列表:

matrix = [[1, 2, 3],
          [2, 3, 3],
          [5, 4, 3]]

np.matrix(list(zip(*matrix)))[::-1]
 Out: matrix([[3, 3, 3], [2, 3, 4], [1, 2, 5]]) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM