I have a problem to solve with a Python code.
- Input ( M ) = 2D array with integer numbers
- Output (T ) same 2D array but with 0 and 1.
- Ti,j = 1 if all the neighbors of the integer Mi,j (in the 8 possible directions) are STRICTLY smaller than the Mi,j.
- Otherwise, in the opposite case, Ti,j = 0 if there is at least one neighbor in the matrix M greater than or equal to Mij.
I try with this code but it doesn't return the correct T.
def findmaximun(M):
# Write your code here
r, c = len(M),len(M[0])
T = [[1 for col in range(c)] for row in range(r)]
for i in range(0,r):
for j in range(0,c):
for k in [-1,0,1]:
if i+k>=0 and i+k<r:
for l in [-1,0,1]:
if j+l>=0 and j+l<c and max(-i-k,0)!=max(-j-l,0):
if M[i][j] <= M[i+k][j+l]:
T[i][j]=0
return(T)