Decision
def sum_neighbors(A, i, j): rows, columns = A.shape r0, r1 = max(0, i-1), min(rows-1, i+1) c0, c1 = max(0, j-1), min(columns-1, j+1) rs = list({r0, i, r1}) cs = [[c] for c in list({c0, j, c1})] return A[rs, cs].sum() - A[i, j]
Explanation
Slice A on the line before and after i with the column before and after j . Take the amount and subtract the cell in i , j . All other codes are for edge processing.
Demonstration
import numpy as np mxn = np.array([[0, 1, 0], [2, 0, 1], [0, 4, 0], [0, 0, 0]]) for i, j in [(0, 0), (1, 0), (1, 1)]: s = "sum of neigbors for i={} and j={} is {}" print s.format(i, j, sum_neighbors(mxn, i, j)) sum of neigbors for i=0 and j=0 is 3 sum of neigbors for i=1 and j=0 is 5 sum of neigbors for i=1 and j=1 is 8