Incrementing part of an array in numpy

given a (2D) array a, and a smaller (2D) array b, how can you add the smaller array to the bigger array at some offset in numpy?

>>> a = zeros((4,4), dtype=int)
>>> a
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])
>>> b = ones((2,2), dtype=int)
>>> b
array([[1, 1],
       [1, 1]])
>>> a[1:3,1:3] += b
>>> a
array([[0, 0, 0, 0],
       [0, 1, 1, 0],
       [0, 1, 1, 0],
       [0, 0, 0, 0]])

Leave a Reply