How do I exclude NaN values when calculating mean of each row in a matrix?
706 views (last 30 days)
The output for the last row of y returns NaN (a copy of the command window is shown below). How do you exclude the NaN values in the last row in order to output an average of all the real number values?
Guillaume on 8 Sep 2017
Since version 2015a, the max, min, mean, median, sum, var, std, and cov function have included a flag to ignore nans
y = mean(gpd, 2, 'omitnan')
Note that your loop makes no sense at all. It's averaging just the last column, so not doing any averaging at all. The line above will average all the columns.
OCDER on 8 Sep 2017
Edited: OCDER on 8 Sep 2017
Remove the for loop, as it only does the last column, which can't be averaged.
To take mean with NaN's in it, use José-Luis' suggestion of nanmean (voted your answer up :) ).
y = nanmean(gpd, 2)
This will return a 5x1 matrix of average of gdp for each row.