【问题标题】:Matrix rotation with multiple values具有多个值的矩阵旋转
【发布时间】:2018-12-06 15:45:11
【问题描述】:

我正在使用 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 值分组。

【问题讨论】:

  • 原始矩阵是什么类型的数据?似乎是一个列表,但是是什么?
  • 矩阵维度也不匹配以执行矩阵乘法。
  • @b-fg 数字在 -1000 到 1000 的范围内
  • 那么数字前面的 x,y,z 是什么?
  • 我的意思是,这是一个元组列表还是什么类型的数据?您如何对数据进行分组?

标签: python matrix rotation matrix-multiplication image-rotation


【解决方案1】:

我已经能够使用@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]

【讨论】:

  • 太棒了。接受的答案
猜你喜欢
  • 1970-01-01
  • 2014-12-16
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 2017-08-28
  • 2014-08-24
相关资源
最近更新 更多