function xnan = tonan(x, bad, criterion)
%   function xnan = tonan(x, bad, criterion)
%         x    --> input vector or matrix
%         bad  --> a threshold value
%         criterion --> 'g'reater | 'l'ess | 'a'bsolute value greater |
%                       'e'qual
%         xnan --> x with all elements greater than or equal to the threshold
%                         set to NaN.
%         only the first argument is required; the second and third
%         default to 1e37 and 'a'
%
if nargin < 3,
   criterion = 'a';
   if nargin < 2,
      bad = 1e37;
   end
end

if criterion(1) == 'a',
   ibad = find( abs(x) >= bad);
elseif criterion(1) == 'g',
   ibad = find( x >= bad );
elseif criterion(1) == 'l',
   ibad = find( x <= bad );
elseif criterion(1) == 'e',
   ibad = find( x == bad );
end

xnan = x;
if ~isempty(ibad),
   xnan(ibad) = NaN;
end;

