function [m,n]=mean_nonan(mat)
% MEAN_NONAN   Mean value, ignoring NaN values
% 
%    [MEAN,NVALS] = MEAN_NONAN(X)
%   
%    For matrices, MEAN_NONAN(X) is a row vector containing the median
%    value of each column (ignoring NaN's). When only NaN values are
%    given, result is NaN;
%    This is equivalent to the new matlab function nanmean
% 
%    Gal Chechik (C) 2005

  m = ones(1,size(mat,2))*NaN;
  for i=1:size(mat,2)
    inds = find(~isnan(mat(:,i)));
    n(i) = length(inds);
    if n(i)>0
      m(i) = mean(mat(inds,i));
    end
  end


return

