# How to get rid of Nan when calculating mean

244 visualizaciones (últimos 30 días)
desert_scientist90 el 25 de Oct. de 2019
Respondida: Fabio Freschi el 25 de Oct. de 2019
Hi I am trying to get rid of the nan's on my mean calculation, if I keep them I wont be able to calculate z-scores. I was using m1=nanmean(x) but when I look at the output I still have the nan's on the column.
##### 2 comentariosMostrar NingunoOcultar Ninguno
Shubham Gupta el 25 de Oct. de 2019
Editada: Shubham Gupta el 25 de Oct. de 2019
To find indeces with NaN values you can use:
ind = find(isnan(A)); % where A is the array
To delete NaN values from that array you can use:
A(ind) = []; % replacing A(ind) with empty array
I am not sure if that's what you want? But it would be helpful to share some data, so we can understand the problem more clearly and be able to solve
Fabio Freschi el 25 de Oct. de 2019
Are you saying that the built-in function in failing in the calculation? It seems weird...

Iniciar sesión para comentar.

Robert U el 25 de Oct. de 2019
Hi Emmanuel Gonzalez-Figueroa,
Tested with Matlab 2016b:
A = [1 NaN 2 3]; % mean w/o NaN: 2, w/ NaN: NaN
mean(A,'omitnan') % nanmean does nothing else
mean(A) % includenan-flag by default set
mean(A,'includenan') % explicit flag
If you are working with matrices and you want to calculate mean-values column-wise but some columns are full of NaN values there is no way of omitting deletion of said columns (see comment by Shubham Gupta).
If you want to calculate the mean of all matrix entries, use the above command twice.
A = [1 NaN 2 3;
3 NaN 4 5];
mean(mean(A,'omitnan'),'omitnan')
Kind regards,
Robert
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Más respuestas (2)

Steven Lord el 25 de Oct. de 2019
The 'omitnan' option will ignore NaN values in your data when computing the mean. If your data contains values that result in a NaN being computed during the process of computing the mean then you'll receive NaN.
x = [1 NaN 2 3];
meanNoNaN = mean(x, 'omitnan') % No NaN
x2 = [1 NaN Inf -Inf 2 3];
meanNaN = mean(x2, 'omitnan') % Is NaN despite 'omitnan'
The NaN in the second element of x2 is ignored in the mean calculation. So we're taking the mean of five values: 1, Inf, -Inf, 2, and 3. As part of that mean calculation we need to add those five elements together using sum (as is normal for the standard arithmetic mean) and adding Inf and -Inf together results in NaN.
It's a bit subtle, but note that the documentation for the 'omitnan' option in the documentation for the mean function states "Ignore all NaN values in the input." [emphasis added] not "You can never get a NaN from this function if you specify this option." or something to that effect.
If you need to avoid even computed NaN values you probably want to remove non-finite values from your data before computing the mean. Use isfinite to identify those non-finite values or use rmoutliers to eliminate them.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Fabio Freschi el 25 de Oct. de 2019
Following Steven Lord answer I would usE
mean(A(isfinite(A)))
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Programming 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