function [M,C,ok] = summatrices(M1,M2,Count)
%summatrices: sums two matrices, ignoring the NaN's
%
%  Two matrices are summed, handling NaN's in a "correct"way;
%  compare the values in the same cell:
%  nr   M1     M2      M     C=Count
%  -------------------------------
%   0   NaN    NaN     NaN   +0
%   1   NaN    v2      v2     1
%   2   v1     Nan     v1    +0
%   3   v1     v2      v1+v2 +1
%  -------------------------------
%
%   INPUT:  M1,M2: the two matrices. The indices must be equal.
%           Count: the counter of values in matrix M1
%
%   OUTPUT: M : the summed matrix 
%           C : the number of values summed up
%           ok: any relevant message, when "yes" the summing up was correct
%
%

%% check the matrices

ok = 'yes';
if ~(size(M1) == size(M2))
    ok = 'check the dimensions and indices of the arrays';
end

%% summing

s = size(M1);
M = M1;
C = Count;
for n = 1 : s(1)
    for m = 1 : s(2)
        n1 = 2;
        n2 = 1;
        if ~isnan(M2(n,m))
            if isnan(M1(n,m))
                M(n,m) = M2(n,m);
                C(n,m) = 1;
            else
                M(n,m) = M1(n,m) + M2(n,m);
                C(n,m) = Count(n,m) + 1;
            end
        end
    end
end


