Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merging Numpy Arrays into 2D array

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading