% SCALES REAL MATRICES containing NaN INTO 0 TO 255 (or other) RANGE
%
% Y = scaler2(B);  % scales B to go from 0 to 255
%
% Y = scaler2(B,16) % scales B to go from 0 to 15
% Y = scaler2(B,10) % gives matrix of single digit integers
%
% Y = scaler2(B,256,max(max(B))/100); % values < 1/100 of peak set to 1/100 peak
%
% See also scaler2.m    (threshold useful in log version)

function Y = dummy_name(X,levels_per_pixel,threshold)

if nargin < 2
  levels_per_pixel = 256;
end%if

if nargin == 3
  error('you should try using ``clip.m'''' since this not working too well now')
  %threshold = threshold*max(max(X)); %unnormalize(threshold is normalized 0..1)
  X = X.*(X>threshold) + threshold*(X<=threshold); % threshold is un normalized
                                                   % eg. say X goes -128 to +127
end%if

minX=min2nan(X);
maxX=max2nan(X);

Y = round( (X-minX)*(levels_per_pixel-1)/(maxX-minX) );

