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.
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.
output: