function mn = mean_misval(A,MODE)

% mn = mean_misval(A,MODE)

nr_mis_val = sum(sum(~finite(A)));

if nr_mis_val == 0
   if MODE==1
      mn=(min(A,[],2)+max(A,[],2))/2;
   elseif MODE==2
      mn=mean(A,2);
   else
      mn=median(A,2);
   end
else
   if MODE==1
      mn=(min(A,[],2)+max(A,[],2))/2;
   elseif MODE==2
      Q=sum(finite(A),2);
      Q(find(Q==0))=NaN;
      A(find(~finite(A)))=0;
      mn=sum(A,2)./Q;
   else
      Nrrow=size(A,1);
      for row=1:Nrrow
         M=median(A(row,find(finite(A(row,:)))));
         if ~isempty(M)
            mn(row,1)=M;
         else
            mn(row,1)=NaN;
         end
      end
   end
end

