mean value of a group of data with NaNs
7 views (last 30 days)
Show older comments
I have a matrix A with the dimension of 12x360x180. It stores 12 values at each grid of the 360x180. Now I want to calculate the average value at each grid point. The problem is that there are unknown number of NaNs. Sometimes all 12 values are non-NaNs, sometimes all of them are NaNs, sometimes a portion of the 12 values are NaNs.
In this case, how do I estimate the mean values at each point of the grid? I know if there are no NaNs, the calculation is as easy as B = mean(A);
Thank you.
0 Comments
Accepted Answer
the cyclist
on 27 Jun 2016
Edited: the cyclist
on 27 Jun 2016
If you have the Statistics and Machine Learning Toolbox, you can use the nanmean function, which computes the mean while ignoring NaNs.
More Answers (2)
Chris Turnes
on 27 Jun 2016
You can also just use the 'omitnan' option in "mean":
A = [1 0 0 1 NaN 1 NaN 0];
M = mean(A,'omitnan')
M =
0.5000
3 Comments
the cyclist
on 27 Jun 2016
If you do not have that Statistics and Machine Learning Toolbox, this should work:
notNan = not(isnan(A));
B = zeros(size(A));
B(notNan) = A(notNan);
sum(B)./sum(notNan)
You can do the sum over different dimensions, as required.
See Also
Categories
Find more on Measurements and Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!