Home > Net >  Merging Numpy Arrays into 2D array
Merging Numpy Arrays into 2D array

Time:04-28

I have 2 numpy arrays like this:

    a = [[a,b,c],
         [d,e,f]]
    b = [[g,h,i],
        [k,l,m]]

I want to merge them into another numpy array, something like following:

c = [[[a,g],[b,h],[c,i]],
    [[d,k],[e,l],[f,m]]]

How can I do it?

CodePudding user response:

You can use the dstack function, i.e.

a = np.array([[1,2,3],
     [4,5,6]])
print(a)
b = np.array([[10,20,30],
    [40,50,60]])
print(b)

c = np.dstack((a,b))
print(c)

which would outputs

[[1 2 3]
 [4 5 6]]
[[10 20 30]
 [40 50 60]]
[[[ 1 10]
  [ 2 20]
  [ 3 30]]

 [[ 4 40]
  [ 5 50]
  [ 6 60]]]
  • Related