Reputation: 41
These are my arrays
image = [[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,1,1,1,1,1,1,0,0],
[0,0,1,1,1,1,1,1,0,0],
[0,0,1,1,1,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
]
image1 = [[0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0]
]
I want to get the average of each element from the different arrays and make a list of new averages, please help
This is what I have done so far:
Upvotes: 0
Views: 155
Reputation: 194
Numpy ninja code.
import numpy as np
image = [[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,1,1,1,1,1,1,0,0],
[0,0,1,1,1,1,1,1,0,0],
[0,0,1,1,1,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
]
image1 = [[0,0,0,0,0,0,0,0,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0],
[0,0,1,0,0,0,1,1,0,0]
]
images = [image,image1]
np.array(images).mean(axis=0)
Upvotes: 1
Reputation: 2133
In addition to the other answers, your code would have worked as following:
imagi = []
for x, y in zip(img, img1):
z = (x + y) / 2
imagi.append(z)
Upvotes: 0
Reputation: 183
import numpy as np
ans = (np.array(image) + np.array(image1))/2.0
ans = ans.tolist()
but it seems to be a duplicate question Average values in two Numpy arrays
Upvotes: 2