So I have the following code with given input:
import numpy as np
x = np.matrix([[1,1,1,1,0],
[1,1,1,0,0],
[1,1,0,0,0],
[1,0,0,0,0]])
print(x)
def MIZ(mat,check):
for j in range(0,mat.shape[0]):
for i in range(0,mat.shape[1]):
try:
if mat[i,j] == 1:
if mat[i+check,j] == 0 or \
mat[i-check,j] == 0 or \
mat[i,j+check] == 0 or \
mat[i,j-check] == 0:
mat[i,j] = 2
except:
pass
return mat
print(MIZ(x,1))
The idea is is quite simple; in that all 1s that lie next to 0s are converted to 2s. The "check" parameter here should ideally be adjustable such that if "check=2" 1s that have a 0 a space away are also converted to a 2, and so on. Now the problem arises with the first element in the matrix (index = 0,0), because then i-1 and j-1 are both -1 and for my purposes I want to avoid this and limit it ONLY to surrounding elements.
The current result of the code is:
[[2 2 2 2 0]
[2 1 2 0 0]
[2 2 0 0 0]
[1 0 0 0 0]]
There's also the issue of the lowermost 1 not being converted to 2 but that's a secondary issue.