1

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

Bram Dekker
  • 631
  • 1
  • 7
  • 19
John
  • 13
  • 2
  • EDIT: I am very sorry, my question lacks a critical piece of information. I would like to extract the values in the intersection region as a new array, Array C. – John Oct 31 '20 at 23:03
  • I finally found a solution to the problem here: https://stackoverflow.com/questions/642763/find-intersection-of-two-nested-lists?rq=1 – John Nov 01 '20 at 09:14

1 Answers1

1

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.

BrandyBerry
  • 136
  • 4
  • 1
    BTW, it can be expressed even shorter as `c = a[:b.shape[0], :] + b[:, :a.shape[1]]` – Arty Oct 31 '20 at 21:16
  • Also to be strict on picture intersection with `a` takes last rows of `a` not first. I.e. correct way is `c = a[max(0, a.shape[0] - b.shape[0]):, :] + b[:, :a.shape[1]]`. Would be nice if you provide both of variants of solutions, for upper rows and for lower. – Arty Oct 31 '20 at 21:18