Let's say I have a two ndarray define as such:
import numpy as np
mask = np.array([[1,1],[1,2]])
values = np.array([[1., 3.],[2., 2.]])
My goal is to calculate the mean of the values based on the mask regions indicated by the integer in mask. Naturally, I would use a for-loop:
out = np.zeros(len(np.unique(mask)))
for j,i in enumerate(np.unique(mask)):
out[j] = np.nanmean(values[mask==i])
However, this serialized solution becomes very slow for large, multidimensional arrays. Is there a way to vectorize this operation efficiently? Thank you for your help in advance!