Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'M*N matrix'
- [[0 1 0]
- [2 0 1]
- [0 4 0]
- [0 0 0]]
- 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]
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement