我正在使用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值分组。
答案 0 :(得分:0)
我已经能够使用有关numpy.rot90
的@DatHydroGuy建议来做到这一点。这是一个如何做的例子。请注意,我首先将列表中的元组中的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]