function ave = meanFixNaN(x,nansub)
% meanFixNaN  Compute mean of a vector having NaN
%             values by removing or replacing NaN

if nargin<2, nansub = -1;  end  %  Default: Remove NaNs

if nansub == -1
  y = x( ~isnan(x) );
else
  y = fixnan(x,nansub);
end

ave = mean(y);

end

% ------------------
function y = fixnan(x,nansub)
% fixnan  Replace NaNs with another value

y = x;
y( isnan(y) ) = nansub;

end