MATLAB: Add two matrices (element wise addition) together ignoring NaNs

matrix addition ignoring nans

Hi all,
Can someone help on this? I'm sure it is straight forward but getting stumped. I would simply like to add two matrices together, element-wise, ignoring the NaNs. I realize one solution is to replace NaNs with zeros but I have some reported zeros that I want to be able to later identify. A solution to that is to set any real zeros to extremely small numbers. Rather than doing the above I am wondering if there is any other way to ignoring the NaNs — I know of sum(…,'omitnan') and nansum() but they work on one matrix or if I concatenate the two matrices together then they aggregate either all rows or all columns depending on the dimension I select.
For example: A = ones(10,3); B = ones(10,3);
% inserting some NaNs and zeros A([1 6 10],1) = NaN; A([3 7],[2 3]) = NaN; B([3 7],2) = 0;
C = A + B;
I would like C = [1 2 2; 2 2 2; 2 0 1; 2 2 2; 2 2 2; 1 2 2; 2 0 1; 2 2 2; 2 2 2; 1 2 2]

Best Answer

Ok, not to waste too much of the MathWorks communities' time but i did find what looks to be the appropriate solution:
tmp = cat(3,A,B); C = nansum(tmp,3);