m = np.array([[[1,2],[2,1]], [[1,1],[2,1]]])
array([[[1, 2],
[2, 1]],
[[1, 1],
[2, 1]]])
m2 = np.array([[[0.4,0.5],[0.2,0.3]], [[0.3,0.4],[0.5,0.5]]])
array([[[ 0.4, 0.5],
[ 0.2, 0.3]],
[[ 0.3, 0.4],
[ 0.5, 0.5]]])
How can I calculate the mean of m2 for each unique element of m? I could write a list comprehension to loop through the elements of m, but I was wondering if there was a more straightforward way to do this. Is np.ma.masked_array appropriate in this case?
This is what I'm trying to achieve
1: np.mean(m2[m==1])
2: np.mean(m2[m==2])
this is fine for a couple of elements, but if I have 100, then it becomes cumbersome. I could write a list comprehension, but was hoping for a better alternative.
mandm2are related