How can I average multiple matrices by element, ignoring NaN values, to create a new matrix of the same size?

I have 248 matrices, each of size 72x144. There are some NaN values inserted randomly here and there in these matrices. I would like to create a new matrix, also of size 72x144, which is an average by element of the originals, but ignoring the NaN values. For example:
Original:
matrixA = [2 3 5 | 3 NaN 1 | 2 4 3]
matrixB = [3 4 NaN | 1 2 5 | NaN 3 5]
matrixC = [NaN 2 3 | 2 5 3 | 1 2 1]
Want:
matrixAvg = [2.5 3 4 | 2 3.5 3 | 1.5 3 3]
Is there a simple way for me to do this?

4 comentarios

I'm not sure what that means exactly, but the matrices are in my workspace, entitled "tsjul100," "tsjul103," "tsjul106"..."tsjul121," "tsjul200," etc., representing a 72x144 set of data taken every 3 hours for 31 days. Does that answer your question?
Does your matrix have any element as zero?
There are no elements as zero, only NaN.

Iniciar sesión para comentar.

 Respuesta aceptada

Hi, if you have Statistics Toolbox, you can use nanmean as follows:
matrixA = [2 3 5 ; 3 NaN 1 ; 2 4 3];
matrixB = [3 4 NaN ; 1 2 5 ; NaN 3 5];
matrixC = [NaN 2 3 ; 2 5 3 ; 1 2 1];
allData = cat(3,matrixA,matrixB,matrixC);
nanmean(allData,3);

2 comentarios

Thank you so much, that worked immediately!
You can also use mean with 'omitnan'
S = mean(..., MISSING) specifies how NaN (Not-A-Number) values are
treated. The default is 'includenan':
'includenan' - the mean of a vector containing NaN values is also NaN.
'omitnan' - the mean of a vector containing NaN values is the mean
of all its non-NaN elements. If all elements are NaN,
the result is NaN.

Iniciar sesión para comentar.

Más respuestas (1)

Edit
matrixA = [2 3 5 ; 3 NaN 1 ; 2 4 3]
matrixB = [3 4 NaN ; 1 2 5 ; NaN 3 5]
matrixC = [NaN 2 3 ; 2 5 3 ; 1 2 1]
A={matrixA matrixB matrixC}
B=cat(3,A{:})
idx=find(isnan(B))
C=ones(size(B));
C(idx)=0;
B(idx)=0;
out=sum(B,3)./sum(C,3)

8 comentarios

This is giving me a new matrix of size 72x144x248. I need one of only 72x144.
No, you can check for the above example,
size(out)
There is just one mistake
out=sum(B,3)./sum(C,3) % instead of .*
out =
2.5000 3.0000 4.0000
2.0000 3.5000 3.0000
1.5000 3.0000 3.0000
Actually, yes, you are correct. I was looking at "C" as my final result instead of "out." However, now I have noticed that "out" is the correct size, but contains the wrong numbers. To try and solve this problem, I decided:
outfinal=out./248
This is giving me approximately correct numbers, but with some anomalously low numbers as well. Can you explain what I might be doing wrong?
Look at edited answer, I've mistaken by using .* instead of ./
Has your code replaced NaN values with zeroes?
The code replace NaN by zeros, but when calculating the mean, it is ignored, for example
[ nan 2 3] % is replaced by
[0 2 3]
% to calculate the mean
sum([0 2 3])./sum([0 1 1]) % =5/2=2.5
Thank you for your help Azzi, I have solved the problem.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Preprocessing en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Oct. de 2013

Comentada:

el 14 de Abr. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by