I have a 2D array of shape 10x10 and I need to find the neighbors of a maximum value in a 2D array. My code is:
import numpy as np array = np.random.randint(-10,10, size=(10,10)) max_index = np.argmax(array) def get_coordinate_i(value): i = 0 if value % 10 >= 1: i = value // 10 return i j = max_index - 10*get_coordinate_i(max_index) i = get_coordinate_i(max_index) def fun(i, j, array): arr = np.array([]) if i!=0 and j!=0 and i!=9 and j!=9: for val in range(-1,2): for val2 in range(-1,2): arr = np.append(arr, array[i+val][i+val2]) return np.reshape(arr, (3,3)) Though, it is not working properly every time.