【问题标题】:How to get all the values of neighbours around an element in matrix?如何获取矩阵中元素周围的所有邻居值?
【发布时间】:2018-08-19 00:19:48
【问题描述】:

我需要在 python 的矩阵中获取一个元素周围所有邻居的值。假设我有一个像下面这样的矩阵,

  matrix=[[1,2,3,4],
             [5,6,7,8],
             [9,10,11,12]]

对于第一个元素,即matrix[0][0],邻居是[2,5,6]

对于matrix[0][1],邻居是[1,3,5,6,7]

对于matrix[0][2],邻居是[2,4,6,7,8]

对于给定的元素,我需要获取这些值列表。

当 i=0,j=0 时,我可以通过比较 i,j 的值来做同样的事情,使用 switch case 和所以。但它会变成冗长的代码。是否有任何内置函数或模块可以使上述任务更加简单?

【问题讨论】:

  • 你可以使用 numpy 吗?
  • 那么期望的输出是什么?列表列表?输入是什么?整个,错误,矩阵?单元格(x, y)的位置?
  • 是的@MadPhysicist。我可以使用 numpy。您可以建议使用 numpy 的任何更简单的解决方案吗?
  • @MrT ,输入是单元格的索引,即 matrix[row][column] 。输出应该是与特定单元格相邻的数字列表。
  • @Ssukumar。如果可以的话我会想办法的

标签: python python-3.x list matrix nearest-neighbor


【解决方案1】:

如果不关心效率,请使用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]

例如

>>> matrix=[[1,2,3,4],
... [5,6,7,8],
... [9,10,11,12]]
>>>
>>> nb_vals(matrix, [1,1])
[1,2,3,5,7,9,10,11]

这与维度无关(适用于输入“矩阵”的任意数量的维度)并处理您可能希望在周围找到邻居的任意数量的索引。

>>> 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 数组?
  • @MadPhysicist scipynumpy 数组是相同的。numpyscipy 的子集。
  • 虽然这是真的,但您提出的工具使用非常糟糕
  • @MadPhysicist 您可能想详细说明您的意思。对于用户想要为邻居选择的任意数量的维度和任意数量的元素的数组,我相信这样做会更干净。到目前为止,我根本看不出你发表评论的意义。
  • 是的,很抱歉。我太沉迷于“矩阵”而没有注意到与维度无关的部分。以为是矫枉过正。投票翻转。
【解决方案2】:

您可以在矩阵中建立坐标和对应值的字典,以便更简单地查找:

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]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多