Search code examples
arraysnumpyassign

Assign a subset of a 2D array by another array of different size


I have an array and I would like to fill a part of the array with anoher one (smaller). For example:

A = np.array([[0,0,0,0,0,0],[0,0,0,0,0,0]])
B = np.array([[1,2,3,4],[1,2,5,9]])

I would like to get:

np.array[[1,2,3,4,0,0],[1,2,5,9,0,0]])]

Solution

  • the answer is

    A[:B.shape[0],B.shape[1]]+=B