function  code = recode(data,key)

% RECODE  --  Arbitrary many-to-one mapping
%
% code = recode(data,key)
%
% DATA is a numerical array of any shape. KEY is a list of triples
% of the form  [min max code].  The function goes through the triples
% in order and replaces all DATA values b/n MIN and MAX with CODE.
% All tests are done via ">=" and "<=". Use +/-Inf to specify open ranges.
% The default code for non-covered DATA values is 0.
%
% Example:  recode([1:10],[-Inf +Inf 99 ; 2 8 5 ; 4 9 3 ; 5 5 0])
%   ans =   [99 5 5 3 0 3 3 3 3 99]
%
% See also SPLIT, AGGREG.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2000/12/22 10:00 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2006, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

code = zeros(size(data)) ;

for triple = key'
  m = triple(1) ;
  M = triple(2) ;
  c = triple(3) ;
  idx = find(data>=m & data<=M) ;
  code(idx) = c ;
end

%--- Return CODE
%%%%% End of RECODE.M
