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

Merge two NumPy arrays into one

Say I have the following two arrays, a and b:

a = array([[[1, 0],
            [1, 1]],

           [[1, 0],
            [0, 0]]

           [[0, 0],
            [1, 0]]])

b = array([[[0, 2],
            [0, 0]],

           [[0, 0],
            [0, 2]]

           [[0, 2],
            [0, 2]]])

and I wish to ‘overlap’ them so that I get the following result:

          [[[1, 2],
            [1, 1]],

           [[1, 0],
            [0, 2]]

           [[0, 2],
            [1, 2]]]

In the case there is an overlapping co-ordinate, I would just take 1. How could I achieve this?

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 :

Using a simple sum I manage to get the desired result:

import numpy as np

a = np.array([[[1, 0],
             [1, 1]],

            [[1, 0],
             [0, 0]],

            [[0, 0],
             [1, 0]]])

b = np.array([[[0, 2],
             [0, 0]],

            [[0, 0],
             [0, 2]],

            [[0, 2],
             [0, 2]]])

print(a+b)

Output:

[[[1 2]
  [1 1]]

 [[1 0]
  [0 2]]

 [[0 2]
  [1 2]]]
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