Finding neighbors in a matrix and storing those neighbors in a new matrix

Question:

What I have is a matrix of characters that looks like this:

matrix = [
    ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'],
    ['-', '-', '-', '+', '-', '-', '-', '+', '-', '-', '-', '+', '-', '-', '+', '-', '-', '-', '-', '-'],
    ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-'],
    ['+', '+', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-'],
    ['-', '+', '-', '+', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-'],
    ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'],
    ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-', '-', '+'],
    ['-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-', '-', '+'],
    ['+', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-'],
    ['-', '-', '+', '-', '-', '-', '-', '-', '-', '-', '-', '+', '-', '-', '-', '-', '-', '-', '+', '-']
]

What I want is to take all 8 neighbors of each index and I want to store these neighbors in a new matrix called neighbourMatrix, but the problem is that, for whatever reason, the elements being characters is causing some issues.

More specifically, I am getting the error:

ValueError: could not convert string to float: '-'

My function code is as follows:

def getNeighbours(matrix, neighbourMatrix):
    """this function will define and store all 8 neighboring cells in a new matrix"""
    for i in range(len(matrix)):
        # loop through rows and columns of matrix
        for j in range(len(matrix[0])):
            # if there is no "lower bound neighbour", jump to last row
            if i == 0:
                neighbourMatrix[i][j] = matrix[len(matrix) - 1][j - 1]
                neighbourMatrix[i][j + 1] = matrix[len(matrix) - 2][j]
                neighbourMatrix[i][j + 2] = matrix[len(matrix) - 2][j + 1]
            else:
                neighbourMatrix[i][j] = matrix[i - 1][j - 1]
                neighbourMatrix[i][j + 1] = matrix[i - 1][j]
                neighbourMatrix[i][j + 2] = matrix[i - 1][j + 1]

            # if there is no "lower bound neighbour", jump to last column
            if j == 0:
                neighbourMatrix[i][j] = matrix[i - 1][len(matrix[0]) - 2]
                neighbourMatrix[i][j + 3] = matrix[i][len(matrix[0]) - 2]
                neighbourMatrix[i][j + 5] = matrix[i + 1][len(matrix[0]) - 2]
            else:
                neighbourMatrix[i][j] = matrix[i - 1][j - 1]
                neighbourMatrix[i][j + 3] = matrix[i][j - 1]
                neighbourMatrix[i][j + 5] = matrix[i + 1][j - 1]

            # if there is no "upper bound neighbour", jump to first row
            if (i == len(matrix) - 1):
                neighbourMatrix[i][j + 5] = matrix[0][j - 1]
                neighbourMatrix[i][j + 6] = matrix[0][j]
                neighbourMatrix[i][j + 7] = matrix[0][j + 1]
            else:
                neighbourMatrix[i][j + 5] = matrix[i + 1][j - 1]
                neighbourMatrix[i][j + 6] = matrix[i + 1][j]
                neighbourMatrix[i][j + 7] = matrix[i + 1][j + 1]

            # if there is no "upper bound neighbour", jump to first column
            if (j == len(matrix[0]) - 1):
                neighbourMatrix[i][j + 2] = matrix[i - 1][0]
                neighbourMatrix[i][j + 4] = matrix[i][0]
                neighbourMatrix[i][j + 7] = matrix[i + 1][0]
            else:
                neighbourMatrix[i][j + 2] = matrix[i - 1][j + 1]
                neighbourMatrix[i][j + 4] = matrix[i][j + 1]
                neighbourMatrix[i][j + 7] = matrix[i + 1][j + 1]
    print(neighbourMatrix)

Where matrix is my original matrix of characters, and neighbourMatrix is meant to be the matrix to hold all 8 character neighbors of any given cell in the original matrix.

ALSO: if my algorithm for finding neighbors is wrong, I would greatly appreciate a fix for that as well.

Asked By: Andrew Marra

||

Answers:

If you are working with list, and you want to append all neighbour to the neighbours you could do like this, in any case I wrote a more general code, that you could edit easily based on what you want.

def getNeighbours(matrix):
    m , n  = len(matrix), len(matrix[0])
    neighbourMatrix = [['' for j in range(n)] for i in range(m)]

    def idx_gen(y, x , m, n):
        v = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1),(1, -1), (1, 0), (1, 1)]
        for val in v:
            # if (0 <= y + val[0] < m) and (0 <= x + val[1] < n): # commented to as well wrap on the borders, or you can just increase the bounds  
            yield (y + val[0]) % m , (x + val[1]) % n

    for i in range(m):
        for j in range(n):
            for idx in idx_gen(i, j, m, n):
                neighbourMatrix[i][j] += matrix[idx[0]][idx[1]] 
    return neighbourMatrix

output:

neighbourMatrix[5][10]
'-------+'
Answered By: amirhm
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.