简体   繁体   English

计算矩阵中邻居数量的最佳方法?

[英]Best way to compute amount of neighbours in matrix?

I need to write a function def amountofNeighbours(row, column) that prints the amount of neighbours there are to a certain element in the matrix. 我需要编写一个函数def amountofNeighbours(row, column) ,该函数将到矩阵中某个元素的邻居数量打印出来。 For example, given the matrix [[2, 3, 4], [5, 6, 7], [8, 9, 10]] , there are three neighbours to the element 2 at position [0][0], while there are eight neighbours to the element 6 at position [1][1]. 例如,给定矩阵[[2, 3, 4], [5, 6, 7], [8, 9, 10]] ,元素2在位置[0] [0]上有三个邻居,而元素6在位置[1] [1]上有八个邻居。 I'm not sure what is the best way to handle a problem like this. 我不确定处理这样的问题的最佳方法是什么。 I went through all the possibilities, and this gave me the following: 我经历了所有的可能性,这给了我以下几点:

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

When I called this with amountofNeighbours(1, 1) it gave me the correct answer, namely 3, but if I called it with amountofNeighbours(2,2) the answer should be 8 while it gave me 3. Anyone has an idea for improvement? 当我用amountofNeighbours(1, 1)调用它时,它给了我正确的答案,即3,但是如果我用amountofNeighbours(2,2)调用了它,答案应该是8,而它给了我3。任何人都有改进的想法?

A straight forward way to do it is to say, "If the cell is at corner, it has three neighbors, otherwise if it is on an edge, it has five, otherwise it has 8." 直接做到这一点的方法是说:“如果该单元在拐角处,则有三个邻居,否则,如果在边缘,则有五个,否则有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

Your function as it is designed now does not do what you specified. 现在设计的功能无法执行您指定的功能。 It takes in the number of rows and columns. 它接受行数和列数。 Then it loops through all elements of your matrix and calculates the number of neighbours. 然后,它遍历矩阵的所有元素并计算邻居数。 Then it returns the last value calculated , so the bottom right element of your matrix, which has 3 neighbours indeed. 然后,它返回最后一次计算出 ,因此矩阵的右下角元素实际上有3个邻居。

You should get rid of the loops to get it to do what you want. 您应该摆脱循环,让它做您想要的事情。 To clarify: 澄清:

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

One solution to avoid many IFs is to start from the element and compute an "enlarged" box around it (a 3x3 square) possibly placed partially outside the matrix. 避免许多IF的一种解决方案是从元素开始,并在元素周围计算一个“放大”框(3x3正方形),该框可能部分位于矩阵外部。

Then you clamp the result and return the number of elements minus one: 然后将结果钳位,并返回减去一的元素数:

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

在此处输入图片说明

The image shows the selected element and the enlarged box around it. 该图显示了选定的元素及其周围的放大框。 The min/max operation will cut off the extra squares leaving a 2x3 box resulting in 2*3-1=5 neighbors count. 最小/最大运算将切除多余的正方形,剩下一个2x3的框,导致2 * 3-1 = 5个邻居计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM