具有多个值的矩阵旋转

Matrix rotation with multiple values

我正在使用 Python 3.7.1.

给定以下一维数组(数字在 -1000 到 1000 之间):

[x00,y00,z00, x10,y10,z10, x20,y20,z20, x30,y30,z30,
 x01,y01,z01, x11,y11,z11, x21,y21,z21, x31,y31,z31,
 x02,y02,z02, x12,y12,z12, x22,y22,z22, x32,y32,z32]

我想用这个旋转矩阵来旋转

|0 -1|
|1  0|

想要的输出:

[x30,y30,z30, x31,y31,z31, x32,y32,z32,
 x20,y20,z20, x21,y21,z21, x22,y22,z22,
 x10,y10,z10, x11,y11,z11, x12,y12,z12,
 x00,y00,z00, x01,y01,z01, x02,y02,z02]

我知道如何在普通数组上完成此操作,但我想将 x、y 和 z 值分组。

我已经能够使用@DatHydroGuy 关于 numpy.rot90 的建议来做到这一点。以下是如何操作的示例。请注意,我首先将 x、y、z 值分组到列表中的元组中。然后创建一个元组的 numpy 数组作为对象,旋转它,然后使用列表理解将数组展平为列表。

import numpy as np

a = [5, 0, 3, 3, 7, 9, 3, 5, 2, 4, 7, 6, 8, 8, 1, 6, 7, 7, 8, 1, 5, 9, 8, 9, 4, 3, 0, 3, 5, 0, 2, 3, 8, 1, 3, 3]
sh = (4,3) # Shape

a_tup = [tuple(a[i:i+3]) for i in range(0, len(a), 3)] # Group list into tuples (x,y,z)
print(a_tup)
# [(5, 0, 3), (3, 7, 9), (3, 5, 2), (4, 7, 6), (8, 8, 1), (6, 7, 7), (8, 1, 5), (9, 8, 9), (4, 3, 0), (3, 5, 0), (2, 3, 8), (1, 3, 3)]
b=np.empty(sh, dtype=object) # Initialize numpy array with object as elements (for the tuples) and your shape sh
for j in range(sh[1]): # Assign tuples from list  to positions in the array
  for i in range(sh[0]):
    b[i,j] = a_tup[i+j]
print(b)
# [[(5, 0, 3) (3, 7, 9) (3, 5, 2)]
#  [(3, 7, 9) (3, 5, 2) (4, 7, 6)]
#  [(3, 5, 2) (4, 7, 6) (8, 8, 1)]
#  [(4, 7, 6) (8, 8, 1) (6, 7, 7)]]
c = np.rot90(b)
print(c)
# [[(3, 5, 2) (4, 7, 6) (8, 8, 1) (6, 7, 7)]
#  [(3, 7, 9) (3, 5, 2) (4, 7, 6) (8, 8, 1)]
#  [(5, 0, 3) (3, 7, 9) (3, 5, 2) (4, 7, 6)]]
print([item for sublist in c.flatten() for item in sublist]) # Flatten the numpy array of tuples to a list of numbers
# [3, 5, 2, 4, 7, 6, 8, 8, 1, 6, 7, 7, 3, 7, 9, 3, 5, 2, 4, 7, 6, 8, 8, 1, 5, 0, 3, 3, 7,9, 3, 5, 2, 4, 7, 6]