【问题标题】:Best way to compute amount of neighbours in matrix?计算矩阵中邻居数量的最佳方法?
【发布时间】:2015-10-31 12:19:22
【问题描述】:

我需要编写一个函数def amountofNeighbours(row, column) 来打印矩阵中某个元素的邻居数量。例如,给定矩阵[[2, 3, 4], [5, 6, 7], [8, 9, 10]],元素 2 在位置 [0][0] 处有三个邻居,而元素 6 在位置 [1][1] 处有八个邻居。我不确定处理此类问题的最佳方法是什么。我经历了所有的可能性,这给了我以下信息:

def amountofNeighbours(row, column):
    neighbours = 0
    for i in range(row):
        for j in range(column):
            if i == 0 and j == 0 or i == 0 and j == column - 1:
                neighbours = 3
            elif i == row - 1 and j == 0 or i == row-1 and j == column - 1:
                neighbours = 3

            elif i > 0 and j == 0 or i == 0 and j > 0:
                neighbours = 5

            elif i == row - 1 and j > 0:
                neighbours = 5

            elif j == column - 1 and i > 0:
                neighbours = 5

            else:
                neighbours = 8

    return neighbours

当我用 amountofNeighbours(1, 1) 调用它时,它给了我正确的答案,即 3,但如果我用 amountofNeighbours(2,2) 调用它,答案应该是 8,而它给了我 3。有人有改进的想法吗?

【问题讨论】:

    标签: python matrix


    【解决方案1】:

    一个直接的方法是说,“如果单元格在角落,它有三个邻居,否则如果它在边缘,它有五个,否则它有 8 个。”

    def numberOfNeighbors(rows, columns, row, column):
        topBottom = row in (0, rows-1)
        leftRight = column in (0, columns-1)
        if topBottom and leftRight:
           return 3
        if topBottom or leftRight:
           return 5
        return 8
    

    【讨论】:

      【解决方案2】:

      您现在设计的功能没有按照您指定的方式执行。它接受行数和列数。然后它遍历矩阵的所有元素并计算邻居的数量。然后它返回计算的最后一个值,即矩阵的右下角元素,它确实有 3 个邻居。

      你应该摆脱循环,让它做你想做的事。澄清一下:

      def amountofNeighbours(row, column, n_rows, n_cols):
          neighbours = 0
          if row == 0 and column == 0 or row == 0 and column == n_cols - 1:
              neighbours = 3
          elif row == n_rows - 1 and column == 0 or row == n_rows-1 and column == n_cols - 1:
              neighbours = 3
      
          elif row > 0 and column == 0 or row == 0 and column > 0:
              neighbours = 5
      
          elif row == n_rows - 1 and column > 0:
              neighbours = 5
      
          elif column == n_cols - 1 and row > 0:
              neighbours = 5
      
          else:
              neighbours = 8
      
          return neighbours
      

      【讨论】:

      • 这不起作用。参数不够。如果去掉循环,i 和 j 就没有意义了。
      • 我知道。 ij没用,应该改成rowcolumn
      • 然后你会得到像 if row == row - 1 这样的条件。你需要4个参数。两个用于矩阵的维度,两个用于单元格的坐标。
      • 我不是说删除for 行,我只是说设计中不应该有任何循环。我更新了我的答案以澄清。
      【解决方案3】:

      避免许多 IF 的一种解决方案是从元素开始并计算一个围绕它的“放大”框(一个 3x3 正方形),可能部分放置在矩阵之外。

      然后你钳制结果并返回元素个数减一:

      def neighbors(row, col, rows, cols):
          ra, rb = row-1, row+2
          ca, cb = col-1, col+2
          dx = min(cb, cols) - max(0, ca)
          dy = min(rb, rows) - max(0, ra)
          return dx*dy - 1
      

      图像显示所选元素及其周围的放大框。最小/最大操作将切掉多余的方块,留下一个 2x3 的框,导致 2*3-1=5 个邻居数。

      【讨论】:

        猜你喜欢
        • 2019-04-09
        • 1970-01-01
        • 2012-10-04
        • 2011-10-07
        • 1970-01-01
        • 2016-04-06
        • 1970-01-01
        • 2023-03-28
        • 2017-07-17
        相关资源
        最近更新 更多