Skip to content
Advertisement

Intersection of two 2-D numpy arrays with unequal rows and columns

I have 2 arrays, one of shape (455,98) and a second with shape (182,472). A geometric description is as per the attached image. Is there a pythonic way to do this? I would also be happy to receive guidance on how to write a function to achieve this.

enter image description here

Advertisement

Answer

Don’t know if I understood your question completely. However this code will add the numbers from a and b arrays within the intersection.

import numpy as np

a = np.ones((455,98))
b = np.ones((182,472))

c = a[:b.shape[0], :a.shape[1]] + b[:b.shape[0], :a.shape[1]]

print(c)
print(c.shape)

Could alternatively use something like:

c = np.dstack((a[:b.shape[0], :a.shape[1]], b[:b.shape[0], :a.shape[1]]))

To retrieve both elements from each array.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement