tags:

views:

36

answers:

1

Is there a fast way in numpy to add array A to array B at a specified location?

For instance, if

B = [
    [0, 1, 2],
    [2, 3, 4],
    [5, 6, 7]
]

and

A = [
    [2, 2],
    [2, 2]
]

and I want to add A to B starting from point (0, 0) to get

C = [
    [2, 3, 2],
    [4, 5, 4],
    [5, 6, 7],
]

Of course I can do that via extending the array A to match the shape of B and then using numpy.roll, but it seems unnecessarily slow if the size of A is much much smaller then size of B.

EDIT:

potentially with wrapping around - ie such that bottom row of A is added to the top row of B and top row of A is added to the bottom row of B

A: 

To modify B in place

B[:2,:2] += A

otherwise

C = B.copy()
C[:2,:2] += A
gnibbler
Great! is there any easy way to add a wrap-around though?B[-2:2, -2:2] does not seem to do anything meaningful =(
cheshire