function m = median_nan(a);


% version of median function that returns median
% of non-NaN elements;

m = median(a);
inan = find(isnan(m));
if isempty(inan)
  return
end

[mrow ncol] = size(a);
if mrow == 1 | ncol == 1
  m = median(a(find(~isnan(a))));
  return
end

for ii = inan            
   tmp =  median(a(find(~isnan(a(ii))),ii));
   if (isempty(tmp))
      tmp = NaN;
   end
   m(ii) = tmp;
end    
