1 |
function y = nansum(x) |
2 |
%NANSUM Sum ignoring NaNs. |
3 |
% NANSUM(X) returns the sum treating NaNs as missing values. |
4 |
% For vectors, NANSUM(X) is the sum of the non-NaN elements in |
5 |
% X. For matrices, NANSUM(X) is a row vector containing the sum |
6 |
% of the non-NaN elements in each column of X. |
7 |
% |
8 |
% See also NANMEDIAN, NANSTD, NANMIN, NANMAX, NANMEAN. |
9 |
|
10 |
% Copyright 1993-2000 The MathWorks, Inc. |
11 |
% $Revision: 1.1 $ $Date: 2006/08/12 19:37:26 $ |
12 |
% |
13 |
% $Header: $ |
14 |
% $Name: $ |
15 |
|
16 |
% Replace NaNs with zeros. |
17 |
nans = isnan(x); |
18 |
i = find(nans); |
19 |
x(i) = zeros(size(i)); |
20 |
|
21 |
% Protect against an entire column of NaNs |
22 |
y = sum(x); |
23 |
i = find(all(nans)); |
24 |
y(i) = i + NaN; |
25 |
|