Python 如何得到矩阵中元素周围所有邻域的值?
我需要在python中获得矩阵中元素周围所有邻居的值。假设我有一个如下所示的矩阵Python 如何得到矩阵中元素周围所有邻域的值?,python,python-3.x,list,matrix,nearest-neighbor,Python,Python 3.x,List,Matrix,Nearest Neighbor,我需要在python中获得矩阵中元素周围所有邻居的值。假设我有一个如下所示的矩阵 matrix=[[1,2,3,4], [5,6,7,8], [9,10,11,12]] 对于第一个元素,即矩阵[0][0],相邻元素是[2,5,6] 对于矩阵[0][1],邻域是[1,3,5,6,7] 对于矩阵[0][2],邻域是[2,4,6,7,8] 对于给定的元素,我需要得到这些值的列表 当I=0,j=0得到矩阵[0][1]、矩阵[1][0]、矩阵[1]
matrix=[[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
对于第一个元素,即矩阵[0][0]
,相邻元素是[2,5,6]
对于矩阵[0][1]
,邻域是[1,3,5,6,7]
对于矩阵[0][2]
,邻域是[2,4,6,7,8]
对于给定的元素,我需要得到这些值的列表
当I=0,j=0得到矩阵[0][1]、矩阵[1][0]、矩阵[1][1]时,我可以通过比较I,j值来做同样的事情,使用开关盒等等。但它将成为冗长的代码。是否有任何内置函数或模块使上述任务更简单?您可以在矩阵中建立坐标和相应值的字典,以便更简单地查找:
matrix=[[1,2,3,4],
[5,6,7,8],
[9,10,11,12]]
def get_neighbors(a, b):
d = {(i, c):matrix[i][c] for i in range(len(matrix)) for c in range(len(matrix[0]))}
return filter(None, [d.get(i) for i in
[(a+1, b+1), (a, b+1), (a+1, b), (a-1, b+1), (a-1, b), (a, b-1), (a+1, b-1)]])
cords = [(0, 0), (0, 1), (0, 2)]
results = [get_neighbors(*i) for i in cords]
输出:
[[6, 2, 5], [7, 3, 6, 1, 5], [8, 4, 7, 2, 6]]
如果您不关心效率,请使用
scipy
:
import scipy, scipy.ndimage
def nb_vals(matrix, indices):
matrix = scipy.array(matrix)
indices = tuple(scipy.transpose(scipy.atleast_2d(indices)))
arr_shape = scipy.shape(matrix)
dist = scipy.ones(arr_shape)
dist[indices] = 0
dist = scipy.ndimage.distance_transform_cdt(dist, metric='chessboard')
nb_indices = scipy.transpose(scipy.nonzero(dist == 1))
return [matrix[tuple(ind)] for ind in nb_indices]
e、 g
这是不可知维度的(适用于输入“矩阵”的任意数量的维度),并处理任意数量的索引,您可能希望在其周围找到邻居
>>> arr_shape = (2,3,4,5)
>>> testMatrix = scipy.array(scipy.random.random(arr_shape)*10, dtype=int)
>>> print(testMatrix)
[[[[7 0 0 1 9]
[9 5 8 5 8]
[4 0 0 8 0]
[1 9 1 3 2]]
[[9 2 3 3 5]
[2 3 3 7 9]
[6 5 6 6 2]
[9 1 1 0 0]]
[[8 8 5 7 9]
[9 0 7 7 6]
[3 8 7 6 4]
[8 7 5 5 9]]]
[[[8 9 2 0 0]
[8 3 5 5 2]
[4 0 1 0 3]
[1 0 9 1 3]]
[[6 9 2 5 2]
[2 7 5 5 3]
[6 7 2 9 5]
[4 2 7 3 1]]
[[1 7 7 7 6]
[5 1 4 1 0]
[3 9 4 9 7]
[7 7 6 6 7]]]]
>>> nb_vals(testMatrix, [1,2,2,3])
[3, 7, 9, 6, 6, 2, 1, 0, 0, 7, 7, 6, 7, 6, 4, 5, 5, 9, 5, 5, 3, 2, 9, 5, 7, 3, 1, 4, 1, 0, 4, 7, 6, 6, 7]
此解决方案在类似于二进制数组掩码的图像上使用棋盘式倒角变换,其中
1
等于掩码上的白色像素,0
等于掩码上的黑色像素(背景)。切角变换计算所有白色像素到背景的棋盘距离;距离计算为1的所有像素位置都是相邻的,并且会返回输入阵列上这些位置的值。您是否同意使用numpy?那么所需的输出是什么?名单?输入是什么?整个,呃,矩阵?电池的位置(x,y)
?是@madpysicator。我可以使用numpy。您能推荐使用numpy的更简单的解决方案吗?@MrT,输入是单元格的索引,即矩阵[行][列]。输出应该是一个与特定单元格相邻的数字列表。@Ssukumar。如果我可以的话,我会想办法的。为什么不使用numpy数组呢?@madpysicalistscipy
和numpy
数组是相同的。numpy
是scipy
的一个子集。虽然这是真的,但你建议的数组使用得非常糟糕tools@MadPhysicist你可能想详细说明你的意思。对于用户希望为邻居选择的任意维数和任意元素数的数组,我相信这样做更干净。到目前为止,我完全看不出你评论的意义。是的,很抱歉。我太沉迷于“矩阵”而没有注意到维度不可知的部分。我觉得这太过分了。投票被推翻了。
>>> arr_shape = (2,3,4,5)
>>> testMatrix = scipy.array(scipy.random.random(arr_shape)*10, dtype=int)
>>> print(testMatrix)
[[[[7 0 0 1 9]
[9 5 8 5 8]
[4 0 0 8 0]
[1 9 1 3 2]]
[[9 2 3 3 5]
[2 3 3 7 9]
[6 5 6 6 2]
[9 1 1 0 0]]
[[8 8 5 7 9]
[9 0 7 7 6]
[3 8 7 6 4]
[8 7 5 5 9]]]
[[[8 9 2 0 0]
[8 3 5 5 2]
[4 0 1 0 3]
[1 0 9 1 3]]
[[6 9 2 5 2]
[2 7 5 5 3]
[6 7 2 9 5]
[4 2 7 3 1]]
[[1 7 7 7 6]
[5 1 4 1 0]
[3 9 4 9 7]
[7 7 6 6 7]]]]
>>> nb_vals(testMatrix, [1,2,2,3])
[3, 7, 9, 6, 6, 2, 1, 0, 0, 7, 7, 6, 7, 6, 4, 5, 5, 9, 5, 5, 3, 2, 9, 5, 7, 3, 1, 4, 1, 0, 4, 7, 6, 6, 7]