function [s, n] = sumnan(A, dim) % calculates sum of matrix leaving out nan:s in direction dim. % returns sum s and number of values not nan n. n = sum(~isnan(A), dim); nans = find(isnan(A)); A(nans) = 0; s = sum(A, dim);