I want to generate a twodimensional array in Python and I would like to iterate through each element and take an average. An element i should be averaged using the 8 surrounding array elements (including element i).
I generated the twodimensional array with a frame of zeros using Forming a frame of zeros around a matrix in python.
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
x,y = A.shape
n = 1
B = np.zeros((x+2*n,y+2*n),dtype=int)
B[n:x+n, n:y+n] = A
print(B)
What is the easiest way to take the average?
numpy
(I've added it).