function y = avgnan(x)
%AVGNAN  Average or mean value for elements excluding those given by `NaN'.
%        For vectors, AVGNAN(X)  is the mean value of non-`NaN' elements in X.
%        For matrices, AVGNAN(X) is a row vector containing the mean value
%        of the non-`NaN' elements in each column.
% 
%        See also MEAN, ADDNAN, CORNAN, MAXNAN, MINNAN, SUMNAN.


%        Ellen R. McGrattan, 2-6-97
%        Revised, 2-10-97, ERM

[m,n]  = size(x);
for i=1:n;
  j      = find(~isnan(x(:,i)));
  if ~isempty(j)
    y(i)   = mean(x(find(~isnan(x(:,i))),i));
  else;
    y(i)   = NaN;
  end;
end;
