# I keep getting NaN values when computing the mean and standard deviation of an array

28 visualizaciones (últimos 30 días)
Cai Chin el 6 de En. de 2021
Comentada: Walter Roberson el 8 de En. de 2021
Hi, I am attempting to calculate the mean of all the rows in a cell array called 'XTrain2_all' comprised of 4x30 doubles, however I keep getting NaN values instead of 4 mean values. The same happens when computing the standard deviation. How do I stop this from happening? Thanks in advance.
XV = [XTrain2_all{:}];
mu = mean(XV,2);
sg = std(XV,[],2);
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Walter Roberson el 6 de En. de 2021
Each of your rows has 30 NaN values.
In rows 1 and 2, they are in columns 95221 to 95250.
In rows 3 and 4, they are in columns 94951 to 94980 .
mean() of a value that includes nan is going to be nan, because anything added to nan gives nan as a result.
You can use
XV = [XTrain2_all{:}];
mu = mean(XV, 2, 'omitnan');
sg = std(XV, [], 2, 'omitnan');
##### 8 comentariosMostrar 6 comentarios más antiguosOcultar 6 comentarios más antiguos
Cai Chin el 7 de En. de 2021
Hi Steven, in my situation, the NaN values all follow eachother across 30 values. Therefore, instead of replacing by the previous number, is it possible to replace these 30 values with the 30 values prior to these missing ones rather than just replacing with the last non-missing value?
Walter Roberson el 8 de En. de 2021
Just loop. It isn't worth overthinking the situation.

Iniciar sesión para comentar.

### Categorías

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