function y=nanmean(x,dim)
%y=nanmean(x)
%y=nanmean(x,dim)
%
%calculate a mean of a matrix w/ nans and infs in it, otherwise just the
%same as the normal mean function.

if isempty(x),
   y=nan;
else,

[M,N]=size(x);

if M==1 | N==1
   x=x(:);
   if isempty(find(~isnan(x) & ~isinf(x)))
   y=nan;
   else
   y=mean(x(find(~isnan(x) & ~isinf(x))));
   end;

elseif M>1 & N>1

   for i=1:N
   if isempty(find(~isnan(x(:,i)) & ~isinf(x(:,i)) ))
   y(i)=nan;
   else
   y(i)=mean(x(find(~isnan(x(:,i)) & ~isinf(x(:,i)) ),i));
   end;
   end; %for i loop

end; %else if
end; %outer else
