m = np.array([[[0.4,0.5],[0.2,0.3]], [[0.8,0.1],[0.7,0.9]]])
id = np.array([[[1,2],[2,3]], [[3,1],[3,2]]])
mask = np.array([[[0,1],[1,0]], [[1,1],[1,1]]])
I'd like to calculate the mean of m for each index in id, and only for nonzero elements in mask.
For example for id==3, that would be the mean of [0.8,0.7].
I thought np.ma.masked_array.mean would do the trick but this does not give me the expected output
>> np.ma.masked_array(m[id==3],mask=mask[id==3]).mean()
0.29999999