# How do I exclude NaN values when calculating mean of each row in a matrix?

175 visualizaciones (últimos 30 días)
Jillian Sweatt el 8 de Sept. de 2017
Comentada: Jason Stockton el 9 de Ag. de 2021
gpd=[2014,3.320,3.364,3.532,3.659,3.691,3.695,3.633,3.481,3.403,...
3.182,2.887,2.560;2015,2.110,2.249,2.483,2.485,2.775,2.832,...
2.832,2.679,2.394,2.289,2.185,2.060;2016,1.967,1.767,1.958,...
2.134,2.264,2.363,2.225,2.155,2.208,2.243,2.187,2.230;2017,...
2.351,2.299,2.323,2.418,2.386,2.337,2.281,NaN,NaN,NaN,NaN,NaN];
for a=gpd(:,end)
y=mean(a,2);
end
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?
>>y =
2.5600
2.0600
2.2300
NaN
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (4)

Guillaume el 8 de Sept. de 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.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

José-Luis el 8 de Sept. de 2017
y = nanmean(a,2)
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Jason Stockton el 9 de Ag. de 2021
Note: Matlab Help says nanmean() is not recommended. They recommend using mean() with the 'omitnan' flag like Guillaume shows.
They may plan to remove it in the future. Personally, I like nanmean better.

Iniciar sesión para comentar.

OCDER el 8 de Sept. de 2017
Editada: OCDER el 8 de Sept. de 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.
y =
158.0313
157.2595
157.0539
254.1744
##### 2 comentariosMostrar NingunoOcultar Ninguno
Jillian Sweatt el 8 de Sept. de 2017
Will that still calculate the mean of the remaining numbers in the last row of gpd?
OCDER el 8 de Sept. de 2017
Editada: OCDER el 8 de Sept. de 2017
Actually, I'm looking at your code and this for loop may not be needed:
for a = gpd(:, end)
... %This only does one iteration, where a = last column.
end
In this case, José-Luis' suggestion of nanmean is correct.

Iniciar sesión para comentar.

Changoleon el 5 de Mzo. de 2018
https://www.mathworks.com/help/stats/nanmean.html
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre MATLAB en Help Center y File Exchange.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by