MATLAB: NANMEANs for each (n,m) position over several matrices with the same size

nanmean

Hi all,
I have a number of matrices with the same size. What I need is a nanmean for each position of the matrices over all matrices, i.e.:
A=[1 2 NAN;4 5 6;7 NAN 9] B=[NAN 2 3;NAN NAN 6;7 8 9] C=[NAN NAN NAN; 4 5 6;7 8 9]
Result:
(1,1) = nanmean(1 NAN NAN) (1,2) = nanmean(2 2 NAN) … (3,3) = nanmean(9 9 9)
Thank you!

Best Answer

Not sure if this is what you're asking but:
A=[1 2 NaN;4 5 6;7 NaN 9];
B=[NaN 2 3;NaN NaN 6;7 8 9];
C=[NaN NaN NaN; 4 5 6;7 8 9];
Z = cat(3,A,B,C);
nanmean(Z,3)