我需要找到一个单元格周围所有相邻元素的总和,比如getsumofneighbors(matrix, i, j)
:
'M*N matrix'
[[0 1 0]
[2 0 1]
[0 4 0]
[0 0 0]]
[0][0]
附近元素的和为3
[1][0]
处为5,[1][1]
处为8
是否有Python库可以找到给定单元格旁边所有元素的总和?
如果您不介意依赖于scipy,可以使用scipy.ndimage.convolve
函数,如下所示:
In [475]: a
Out[475]:
array([[0, 1, 0],
[2, 0, 1],
[0, 4, 0],
[0, 0, 0]])
In [476]: kernel
Out[476]:
array([[1, 1, 1],
[1, 0, 1],
[1, 1, 1]])
In [477]: from scipy.ndimage import convolve
In [478]: c = convolve(a, kernel, mode='constant')
In [479]: c
Out[479]:
array([[3, 3, 2],
[5, 8, 5],
[6, 3, 5],
[4, 4, 4]])
uniform_filter
函数的有效卷积核为[[1/9, 1/9, 1/9], [1/9, 1/9, 1/9], [1/9, 1/9, 1/9]]。因此,要获得所需的结果,您需要将输入或输出按9缩放,并减去原始数组。在我看来,这并不比我展示的更容易。 - Warren Weckessernp.sum
来计算特定区域的总和:def getsumofneighbors(matrix, i, j):
region = matrix[max(0, i-1) : i+2,
max(0, j-1) : j+2]
return np.sum(region) - matrix[i, j] # Sum the region and subtract center
请注意,max
的存在是因为负数起始索引会触发不同的切片操作。
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]
将行在i
之前和之后,列在j
之前和之后的切片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
以下函数应该完成查找单元格相邻元素总和的任务:
def sum_around(matrix, r, c):
total = 0
offset = (0, 1, -1)
indices = ((i, j) for i in offset for j in offset)
next(indices, None)
for rec in indices:
try:
row = r - 1 + rec[0]
col = c - 1 + rec[1]
total += matrix[row][col] if 0 <= row and 0 <= col else 0
except IndexError:
continue
return total
关键点:
offsets 定义了给定索引需要的相对变化,以获取周围元素(包括给定索引本身的位置,因为相对变化 [0] [0] 不会改变索引)
由于偏移量中元素的顺序,索引生成器对象是这样创建的,即第一项是 (0, 0)。第一项被 next() 消耗。
循环遍历生成器对象的剩余元素,计算并分配值给矩阵索引,并在索引不为负数(出于明显原因)时将值添加到总和中,在索引超出范围(IndexError)时继续执行。
函数期望用户输入基于手指的索引和扣除 -1 以将值转换为基于 0 的索引。
def sumofnieghbors(MatrixObj, indexR, indexC):
upperleft = 0
if not (indexR < 1) or (indexC < 1):
upperleft = MatrixObj[indexR - 1][indexC - 1]
upper = 0
if not (indexR < 1):
upper = MatrixObj[indexR - 1][indexC]
upperright = 0
if not ((indexR < 1) or (indexC >= NbofCol)):
upperright = MatrixObj[indexR - 1][indexC + 1]
right = 0
if not (indexC >= NbofCol):
right = MatrixObj[indexR][indexC + 1]
rightdown = 0
if not ((indexR >= NbofRow) or (indexC >= NbofCol)):
rightdown = MatrixObj[indexR + 1][indexC + 1]
down = 0
if not (indexR >= NbofRow):
down = MatrixObj[indexR + 1][indexC]
leftdown = 0
if not ((indexR >= NbofRow) or (indexC < 1)):
leftdown = MatrixObj[indexR + 1][indexC - 1]
left = 0
if not (indexC < 1):
left = MatrixObj[indexR][indexC - 1]
return (upperleft + upper + upperright + right + rightdown + down + leftdown + left)
left
,... 变量? - MSeifert
out = np.zeros_like(A); out[1:,1:] += A[:-1,:-1];
等。 - dan-man