I have an array that will print a 10x10 random numbers from 1 to 9. I want to sum every 4 elements horizontally and vertically.
Here is the code:
import numpy as np
random_array = np.random.randint(1, 10, size=(10, 10))
print(random_array)
For example:
[[7 4 5 7 3 9 4 2 5 3]
[2 3 7 1 1 1 4 3 2 8]
[8 2 6 8 8 1 7 8 8 3]
[4 7 8 8 9 5 9 2 9 7]
[2 1 9 2 4 5 4 6 5 2]
[3 3 1 6 4 4 7 9 7 9]
[2 8 3 2 7 8 7 3 9 2]
[4 4 6 5 4 1 7 5 1 1]
[9 5 8 1 9 9 9 1 1 4]
[8 8 6 1 3 6 4 1 8 3]]
What I want to do is add every 4 elements horizontally and vertically. E.g. horizontally a sum of (7, 4, 5, 7) and then the sum of (4, 5, 7, 3) and so on. Then vertically the sum of(7,2,8,4) and sum of (2,8,4,2) and so on.