x,y对的转置展平矩阵

时间:2018-12-08 16:34:03

标签: python matrix transpose

我得到了一个一维十进制数字数组,它们代表x和y值。

我需要转置给定的一维数组,就好像它是矩阵一样。

我当前的代码可以执行此操作,但不完全是我想要的方式:

to_transpose = [0.914, 0.639, 0.058, 0.760, 0.926, 0.475,
                0.255, 0.671, 0.195, 0.966, 0.336, 0.841,
                0.279, 0.341, 0.591, 0.638, 0.520, 0.225]
matrix_width = 6
matrix_height = 3
# INITIALIZE AN EMPTY LIST
transposed_list = [None] * matrix_width * matrix_height

for w in range(matrix_width):
    for h in range(matrix_height):
        transposed_list[w * matrix_height + h] = to_transpose[h * matrix_width + w]

此代码正确地转置了矩阵,但不是我想要的格式。

由于这是一个x,y值的数组,因此所需的输出如下所示:

correct_output=[0.914, 0.639, 0.255, 0.671, 0.279, 0.341,
                0.058, 0.760, 0.195, 0.966, 0.591, 0.638,
                0.926, 0.475, 0.336, 0.841, 0.520, 0.225]

在正确的输出中,转置中每2个小数点被视为1。

  1. 我想知道一次通过是否可行 通过矩阵而不使用外部库,如 上面的例子?
  2. 我希望这能同时处理正方形和非正方形矩阵。

5 个答案:

答案 0 :(得分:1)

当您要求使用“无库”解决方案时,我非常建议您将numpy用于所有与矩阵处理有关的事情,例如重塑或转置,这两个都需要在这里进行:

>>> import numpy as np
>>> to_transpose = [0.914, 0.639, 0.058, 0.760, 0.926, 0.475,
                    0.255, 0.671, 0.195, 0.966, 0.336, 0.841,
                    0.279, 0.341, 0.591, 0.638, 0.520, 0.225]

>>> np.array(to_transpose).reshape((3,3,2)).transpose(1,0,2).ravel()
array([ 0.914,  0.639,  0.255,  0.671,  0.279,  0.341,
        0.058,  0.76 ,  0.195,  0.966,  0.591,  0.638,
        0.926,  0.475,  0.336,  0.841,  0.52 ,  0.225])

打破这一点:

  • np.array将您的列表变成array ...
  • 然后您reshape3x3x2,即3x3元组矩阵...
  • 然后您transpose,交换第一(0)和第二(1)轴并保持第三(2)到位...
  • 最后,矩阵再次用ravel展平

如果最后不能使用numpy,您仍然可以使用它正确地转置索引矩阵以找出哪个元素必须去哪里,然后在列表to_transpose上循环生成该元素。 :

>>> list(np.array(list(range(w*h))).reshape((3,3,2)).transpose(1,0,2).ravel())
[0, 1, 6, 7, 12, 13, 2, 3, 8, 9, 14, 15, 4, 5, 10, 11, 16, 17]

>>> [i%2 + (i//2 * w % (w*h)) + 2 * (i//(h*2)) for i in range(w*h)]
[0, 1, 6, 7, 12, 13, 2, 3, 8, 9, 14, 15, 4, 5, 10, 11, 16, 17]

>>> [to_transpose[i%2 + (i//2 * w % (w*h)) + 2 * (i//(h*2))] for i in range(w*h)]
[0.914, 0.639, 0.255, 0.671, 0.279, 0.341,
 0.058, 0.76, 0.195, 0.966, 0.591, 0.638,
 0.926, 0.475, 0.336, 0.841, 0.52, 0.225]

当然,您也可以对常规循环执行相同的操作,而不是使用列表理解和其他语言。基本上,索引加法中的三个术语中的每一个都对应于矩阵的一个维度,并且说实话,我更多地是通过猜测而不是真正了解正在发生的事情来弄清楚这一点。不用说numpy解决方案更干净。

答案 1 :(得分:1)

如果您坚持不使用numpy,我建议您将x,y值分开,或者先将它们元组在一起:

to_transpose = [0.914, 0.639, 0.058, 0.760, 0.926, 0.475,
                0.255, 0.671, 0.195, 0.966, 0.336, 0.841,
                0.279, 0.341, 0.591, 0.638, 0.520, 0.225]
a = to_transpose

rows = 3
cols = 3
tot = rows*cols

# separated
x, y = a[::2], a[1::2]
xt = [x[i+j] for i in range(0, rows) for j in range(0, tot, cols)]
yt = [y[i+j] for i in range(0, rows) for j in range(0, tot, cols)]
transposed = [e for t in zip(xt, yt) for e in t]

# tupled
xy = [(i,j) for i,j in zip(a[::2], a[1::2])]
xyt = [xy[i+j] for i in range(0, rows) for j in range(0, tot, cols)]
transposed = [e for t in xyt for e in t]

很明显,即使这种方法有效,numpy仍将是进行此类操作的更好工具。

答案 2 :(得分:1)

#!/bin/python3

to_transpose = [
    0.914, 0.639, 0.058, 0.760, 0.926, 0.475,
    0.255, 0.671, 0.195, 0.966, 0.336, 0.841,
    0.279, 0.341, 0.591, 0.638, 0.520, 0.225
]

matrix_width = 6
matrix_height = 3

to_transpose_with_pairs = [(to_transpose[2 * i], to_transpose[2 * i + 1]) for i in range(len(to_transpose) // 2)]
# [
#     (0.914, 0.639), (0.058, 0.76), (0.926, 0.475),
#     (0.255, 0.671), (0.195, 0.966), (0.336, 0.841),
#     (0.279, 0.341), (0.591, 0.638), (0.52, 0.225)
# ]

to_transpose_as_matrix = [None for _ in range(matrix_height)]

for row in range(matrix_height):
    start = row * matrix_width // 2
    end = start + matrix_width // 2

    to_transpose_as_matrix[row] = to_transpose_with_pairs[start:end]

# [
#     [(0.914, 0.639), (0.058, 0.76), (0.926, 0.475)],
#     [(0.255, 0.671), (0.195, 0.966), (0.336, 0.841)],
#     [(0.279, 0.341), (0.591, 0.638), (0.52, 0.225)]
# ]

transposed_as_matrix = list(map(list, zip(*to_transpose_as_matrix)))
# [
#     [(0.914, 0.639), (0.255, 0.671), (0.279, 0.341)],
#     [(0.058, 0.76), (0.195, 0.966), (0.591, 0.638)],
#     [(0.926, 0.475), (0.336, 0.841), (0.52, 0.225)]
# ]

transposed_with_pairs = [pair for row in transposed_as_matrix for pair in row]
# [
#     (0.914, 0.639), (0.255, 0.671), (0.279, 0.341),
#     (0.058, 0.76), (0.195, 0.966), (0.591, 0.638),
#     (0.926, 0.475), (0.336, 0.841), (0.52, 0.225)
# ]


transposed = [val for pair in transposed_with_pairs for val in pair]
# [
#     0.914, 0.639, 0.255, 0.671, 0.279, 0.341,
#     0.058, 0.76, 0.195, 0.966, 0.591, 0.638,
#     0.926, 0.475, 0.336, 0.841, 0.52, 0.225
# ]

答案 3 :(得分:1)

我发现此方法可以按您希望的方式工作,只需遍历矩阵一次,而无需使用任何库。

to_transpose = [0.914, 0.639, 0.058, 0.760, 0.926, 0.475,
                0.255, 0.671, 0.195, 0.966, 0.336, 0.841,
                0.279, 0.341, 0.591, 0.638, 0.520, 0.225]
matrix_width = 6
matrix_height = 3
# INITIALIZE AN EMPTY LIST
transposed_list = [None] * matrix_width * matrix_height

for w in range(0, matrix_width, 2):
    for h in range(matrix_height):
        transposed_list[w * matrix_height + (2 * h)] = to_transpose[h * matrix_width + w]
        transposed_list[(w * matrix_height + (2 * h)) + 1] = to_transpose[(h * matrix_width + w) + 1]
print(transposed_list)

我做了三件事:

  • 通过将阶跃值设为2来使w跳过奇数值
  • 增加了一行,其中奇数索引元素获得y的值
  • 最后将h的值加倍,以便每个备用元素都得到一个 新价值

答案 4 :(得分:0)

您可以在不使用numpy的情况下一次计算一次,但很糟糕:

m=to_transpose # for easy writing only
[ n for l in zip( *[[(m[i],m[i+1]) for i in range(k,k+6,2)] for k in range(0,len(m),6)] ) for t in l for n in t ]

请改用numpy。