NumPy Stack多维数组

问题描述 投票:3回答:1

我有3个nD数组,如下所示

x = [[1, 2, 3],  
     [4, 5, 6],  
     [7, 8, 9]]    


y = [[10, 11, 12],
     [13, 14, 15],
     [16, 17, 18]]

z = [[ 19,  20,  21],
     [ 22,  23,  24],
     [ 25,  26,  27]]

[不使用for循环,我试图将每个2x2矩阵元素附加在一起,这样

a1 = [[1,2]
      [4,5]]

a2 = [[10,11], 
      [13,14]]

a3 = [[19,20],
      [22,23]]

should append to

a = [[1,10,19],[2,11,20],[4,13,22],[5,14,23]]

Please note, the NxN matrix will always be N = j - 1 where j is x.shape(i,j)

Similarly for other 2x2 matrices, the arrays are as follows

b = [[2,11,20],[3,12,21],[5,14,23],[6,15,24]]
c = [[4,13,22],[5,14,23],[7,16,25],[8,17,26]]
d = [[5,14,23],[6,15,24],[8,17,26],[9,18,27]]

对于大型数据集,for循环会影响运行时,因此我试图查看是否有一种使用NumPy堆栈技术的方法

python numpy matrix multidimensional-array
1个回答
0
投票

a1 = np.array([[1,2],[4,5]])

a2 = np.array([[10,11],[13,14]])

a3 = np.array([[19,20],[22,23]])

def everything(a1,a2,a3):

    b1 = a1.reshape(-1)
    b2 = a2.reshape(-1)
    b3 = a3.reshape(-1)
    c = np.concatenate((b1, b2, b3))
    b = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
    def inner(a, i):
        while i < len(a):
            i = i + 1
            return a[i - 1]
    def looping(a, c):
        k = 0
        j = 0
        while j < len(a) - 1:
            i = 0
            while i < len(a):
                b[i][j] = inner(c, k)
                i += 1
                k += 1
            j += 1
    looping(b3, c)
    print(b)
everything(a1,a2,a3)
© www.soinside.com 2019 - 2024. All rights reserved.