python - 以特定方式组合一组数组

我有一组 9 个不同的数组,大小均为 n x n。我需要将它们元素明智地组合起来,以产生一个特定的数组。

例如:给定一组 9 个相同大小的数组:

a1 = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19]])
a2 = np.array([[21, 22, 23], [24, 25, 26], [27, 28, 29]])
a3 = np.array([[31, 32, 33], [34, 35, 36], [37, 38, 39]])

a4 = np.array([[41, 42, 43], [44, 45, 46], [47, 48, 49]])
a5 = np.array([[51, 52, 53], [54, 55, 56], [57, 58, 59]])
a6 = np.array([[61, 62, 63], [64, 65, 66], [67, 68, 69]])

a7 = np.array([[71, 72, 73], [74, 75, 76], [77, 78, 79]])
a8 = np.array([[81, 82, 83], [84, 85, 86], [87, 88, 89]])
a9 = np.array([[91, 92, 93], [94, 95, 96], [97, 98, 99]])

想要的结果是
b = np.array([[11, 21, 31, 12, 22, 32, 13, 23, 33],
              [41, 51, 61, 42, 52, 62, 43, 53, 63],
              [71, 81, 91, 72, 82, 92, 73, 83, 94],
              [14, 24, 34, 15, 25, 35, 16, 26, 36],
              [44, 54, 64, 45, 55, 65, 46, 56, 66],
              [74, 84, 94, 75, 85, 95, 76, 86, 96],
              [17, 27, 37, 18, 28, 38, 19, 29, 39],
              [47, 57, 67, 48, 58, 68, 49, 59, 69],
              [77, 87, 97, 78, 88, 98, 79, 89, 99]]) 

所以数组 b 的第一行由 a1、a2 和 a3 的第一行组成,按元素组合。

数组 b 的第二行由 a4、a5 和 a6 的第一行按元素组合而成。

第三行由第一行 a7、a8 和 a9 组成,按元素组合。

然后对 a1-a9 中的其余行继续相同的模式。

这需要适用于任何大小的 a1-a9 数组,因为数组的大小为 n x n。我试过修改 np.concatenate、zip 和 np.einsum,但都没有成功。

最佳答案

编辑:推广到不同的数组大小和数组数量:

import numpy as np

def combine_arrays(arrays):
    arrays = np.asarray(arrays)
    n, p, q = arrays.shape
    s = int(round(np.sqrt(n)))
    arrays = arrays.reshape(s, -1, p, q)
    return arrays.transpose(2, 0, 3, 1).reshape(s * p, -1)

a1 = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19]])
a2 = np.array([[21, 22, 23], [24, 25, 26], [27, 28, 29]])
a3 = np.array([[31, 32, 33], [34, 35, 36], [37, 38, 39]])

a4 = np.array([[41, 42, 43], [44, 45, 46], [47, 48, 49]])
a5 = np.array([[51, 52, 53], [54, 55, 56], [57, 58, 59]])
a6 = np.array([[61, 62, 63], [64, 65, 66], [67, 68, 69]])

a7 = np.array([[71, 72, 73], [74, 75, 76], [77, 78, 79]])
a8 = np.array([[81, 82, 83], [84, 85, 86], [87, 88, 89]])
a9 = np.array([[91, 92, 93], [94, 95, 96], [97, 98, 99]])

print(combine_arrays([a1, a2, a3, a4, a5, a6, a7, a8, a9]))
# [[11 21 31 12 22 32 13 23 33]
#  [41 51 61 42 52 62 43 53 63]
#  [71 81 91 72 82 92 73 83 93]
#  [14 24 34 15 25 35 16 26 36]
#  [44 54 64 45 55 65 46 56 66]
#  [74 84 94 75 85 95 76 86 96]
#  [17 27 37 18 28 38 19 29 39]
#  [47 57 67 48 58 68 49 59 69]
#  [77 87 97 78 88 98 79 89 99]]

a1 = np.array([[11, 12], [14, 15]])
a2 = np.array([[21, 22], [24, 25]])
a3 = np.array([[31, 32], [34, 35]])
a4 = np.array([[41, 42], [44, 45]])

print(combine_arrays([a1, a2, a3, a4]))
# [[11 21 12 22]
#  [31 41 32 42]
#  [14 24 15 25]
#  [34 44 35 45]]

您可以通过重塑和转置来做到这一点:
import numpy as np

a1 = np.array([[11, 12, 13], [14, 15, 16], [17, 18, 19]])
a2 = np.array([[21, 22, 23], [24, 25, 26], [27, 28, 29]])
a3 = np.array([[31, 32, 33], [34, 35, 36], [37, 38, 39]])

a4 = np.array([[41, 42, 43], [44, 45, 46], [47, 48, 49]])
a5 = np.array([[51, 52, 53], [54, 55, 56], [57, 58, 59]])
a6 = np.array([[61, 62, 63], [64, 65, 66], [67, 68, 69]])

a7 = np.array([[71, 72, 73], [74, 75, 76], [77, 78, 79]])
a8 = np.array([[81, 82, 83], [84, 85, 86], [87, 88, 89]])
a9 = np.array([[91, 92, 93], [94, 95, 96], [97, 98, 99]])

a = np.stack([a1, a2, a3, a4, a5, a6, a7, a8, a9])
a = a.reshape(3, 3, 3, 3).transpose(2, 0, 3, 1).reshape(9, 9)
print(a)
# [[11 21 31 12 22 32 13 23 33]
#  [41 51 61 42 52 62 43 53 63]
#  [71 81 91 72 82 92 73 83 93]
#  [14 24 34 15 25 35 16 26 36]
#  [44 54 64 45 55 65 46 56 66]
#  [74 84 94 75 85 95 76 86 96]
#  [17 27 37 18 28 38 19 29 39]
#  [47 57 67 48 58 68 49 59 69]
#  [77 87 97 78 88 98 79 89 99]]