how to find mean with nan

5 visualizaciones (últimos 30 días)
Fani el 10 de Oct. de 2014
Comentada: Image Analyst el 10 de Oct. de 2014
Hi!
I want to find mean with nan values and with different step. My script is:
RowDivision=[1,46,172,305,472,618,714,784,920];
nElement=diff(RowDivision); % step
meanG=cellfun(@mean,(mat2cell(G,nElement,ones(size(G,2),1)))); %mean
but if i have one only nan value i.e. from 1 to 46 the mean is nan. How can I ignore the nan? Thank you!
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Azzi Abdelmalek el 10 de Oct. de 2014
Editada: Azzi Abdelmalek el 10 de Oct. de 2014
a=[1 2 nan 3 nan 10]
nanmean(a)
%or
mean(a(~isnan(a)))
0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Más respuestas (2)

Andrew Reibold el 10 de Oct. de 2014
Editada: Andrew Reibold el 10 de Oct. de 2014
Use nanmean
If you dont have it, I wrote this
For an array, x
n_nans = 0;
temp = x;
for i = 1:length(x)
if isnan(x(i))
temp(i) = 0;
n_nans = n_nans+1;
end
end
mean = sum(temp)/(length(temp)-n_nans)
1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Andrew Reibold el 10 de Oct. de 2014

Iniciar sesión para comentar.

Image Analyst el 10 de Oct. de 2014
If you have the Stats toolbox, use nanmeans. If you don't, you can do it in two lines of code, one to find the nans and one to get the mean:
% Create sample data
RowDivision=[1,46,172,305,472,618,714,784,920];
% Make some nans
RowDivision(2) = nan;
RowDivision(4) = nan
% Find the nans
nanElements = isnan(RowDivision)
% Compute the mean with no nans being considered.
meanNoNans = mean(RowDivision(~nanElements))
Actually you can do that in one line of code if you want.
1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Image Analyst el 10 de Oct. de 2014
Oops - just like Azzi's edit which he posted while I was composing my response.

Iniciar sesión para comentar.

Categorías

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