Home > FMAToolbox > General > MatchPairs.m

MatchPairs

PURPOSE ^

MatchPairs - Pair nearest values in two lists.

SYNOPSIS ^

function pairs = MatchPairs(list1,list2,varargin)

DESCRIPTION ^

MatchPairs - Pair nearest values in two lists.

  USAGE

    pairs = MatchPairs(list1,list2,<options>)

    list1          first list
    list2          second list
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'error'       maximum error (default = 0)
    =========================================================================

  OUTPUT

    pairs        list of matched pairs (NaN indicates no match)

  NOTE

    This function uses list1 as the reference and is generally not symmetrical.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function pairs = MatchPairs(list1,list2,varargin)
0002 
0003 %MatchPairs - Pair nearest values in two lists.
0004 %
0005 %  USAGE
0006 %
0007 %    pairs = MatchPairs(list1,list2,<options>)
0008 %
0009 %    list1          first list
0010 %    list2          second list
0011 %    <options>      optional list of property-value pairs (see table below)
0012 %
0013 %    =========================================================================
0014 %     Properties    Values
0015 %    -------------------------------------------------------------------------
0016 %     'error'       maximum error (default = 0)
0017 %    =========================================================================
0018 %
0019 %  OUTPUT
0020 %
0021 %    pairs        list of matched pairs (NaN indicates no match)
0022 %
0023 %  NOTE
0024 %
0025 %    This function uses list1 as the reference and is generally not symmetrical.
0026 
0027 
0028 
0029 
0030 
0031 
0032 % ***************** TODO: Change PlotRippleStats to use Match instead of MatchPairs, then remove MatchPairs
0033 
0034 
0035 
0036 
0037 
0038 % Copyright (C) 2004-2011 by Michaƫl Zugaro
0039 %
0040 % This program is free software; you can redistribute it and/or modify
0041 % it under the terms of the GNU General Public License as published by
0042 % the Free Software Foundation; either version 3 of the License, or
0043 % (at your option) any later version.
0044 
0045 % Default values
0046 err = 0;
0047 
0048 % Check number of parameters
0049 if nargin < 2 | mod(length(varargin),2) ~= 0,
0050   error('Incorrect number of parameters (type ''help <a href="matlab:help MatchPairs">MatchPairs</a>'' for details).');
0051 end
0052 
0053 % Check parameter sizes
0054 if ~isdvector(list1),
0055     error('Parameter ''list1'' is not a vector (type ''help <a href="matlab:help MatchPairs">MatchPairs</a>'' for details).');
0056 end
0057 if ~isdvector(list2),
0058     error('Parameter ''list2'' is not a vector (type ''help <a href="matlab:help MatchPairs">MatchPairs</a>'' for details).');
0059 end
0060 if size(list2,1) == 1,
0061     list2 = list2';
0062 end
0063 
0064 % Parse parameter list
0065 for i = 1:2:length(varargin),
0066   if ~ischar(varargin{i}),
0067     error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help MatchPairs">MatchPairs</a>'' for details).']);
0068   end
0069   switch(lower(varargin{i})),
0070     case 'error',
0071       err = varargin{i+1};
0072       if ~isdscalar(err,'>0'),
0073         error('Incorrect value for property ''error'' (type ''help <a href="matlab:help MatchPairs">MatchPairs</a>'' for details).');
0074       end
0075     otherwise,
0076       error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help MatchPairs">MatchPairs</a>'' for details).']);
0077   end
0078 end
0079 
0080 i = 1;
0081 i2 = 1;
0082 for i1 = 1:length(list1),
0083     % disp(['(' int2str(i1) ') ' num2str(list1(i1)) ' - (' int2str(i2) ') ' num2str(list2(i2)) ' ?']);
0084     while list1(i1) > list2(i2) + err,
0085         % disp([' (' int2str(i1) ') ' num2str(list1(i1)) ' > (' int2str(i2) ') ' num2str(list2(i2)) ' + ' num2str(err)]);
0086         pairs(i,:) = [NaN list2(i2)];
0087         i = i+1;
0088         i2 = i2+1;
0089     end
0090     if list1(i1) < list2(i2) - err,
0091         % disp([' (' int2str(i1) ') ' num2str(list1(i1)) ' < (' int2str(i2) ') ' num2str(list2(i2)) ' - ' num2str(err)]);
0092         pairs(i,:) = [list1(i1) NaN];
0093         i = i+1;
0094     else
0095         % disp([' (' int2str(i1) ') ' num2str(list1(i1)) ' ~ (' int2str(i2) ') ' num2str(list2(i2)) ' +- ' num2str(err)]);
0096         pairs(i,:) = [list1(i1) list2(i2)];
0097         i2 = i2+1;
0098         i = i+1;
0099     end
0100 end
0101 n = length(list2)-i2+1;
0102 if n > 0,
0103     pairs = [pairs ; repmat(NaN,n,1) list2(i2:end)];
0104 end

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005