MATLAB: Mean of values above a threshold

mean values above threshold

I have a 3D data (X,Y,t)(900x804x366). I want to calculate the time mean over time, but only taking into account the values that exceed a certain threshold. I have looked for that over the web, but I didn't find a way to do it.
Thanks a lot for the assistance.

Best Answer

Data = rand(900, 804, 366);
mask = (Data > 0.65);
Data(~mask) = 0;
meanData = sum(Data, 3) ./ sum(mask, 3);
This means: Set all unwanted data to zero, sum up, do not divide by the length of the dimension to operate on, but on the number of selected elements.
Alternatively you can set the values to be ignored to NaN and use nanmean afterwards, or mean(..., 'omitnan'). But these functions replace the NaNs with zeros again and perform the above calculation. So you can do this directly also.