function y = nan_mean(x)
% NAN_MEAN - return the mean ignoring NaNs
%

% 
%	$Revision: 1.4 $
%	$Date: 2008/02/18 19:37:18 $
%	$Id: nan_mean.m,v 1.4 2008/02/18 19:37:18 duncombe Exp duncombe $
% 
% COMMENTS - There is a matlab function nanmean which does this
% 

if isempty(x) % Check for empty input.
    y = NaN;
    return
end

% Replace NaNs with zeros.
nans = isnan(x);
i = find(nans);
x(i) = zeros(size(i));

if min(size(x))==1,
  count = length(x)-sum(nans);
else
  count = size(x,1)-sum(nans);
end

% Protect against a column of all NaNs
i = find(count==0);
count(i) = ones(size(i));
y = sum(x)./count;
y(i) = i + NaN;

