Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slmetric_pw
Home > sltoolbox > core > slmetric_pw.m

slmetric_pw

PURPOSE ^

SLMETRIC_PW Compute the metric between column vectors pairwisely

SYNOPSIS ^

function M = slmetric_pw(X1, X2, mtype, varargin)

DESCRIPTION ^

SLMETRIC_PW Compute the metric between column vectors pairwisely

 $ Syntax $
   - M = slmetric_pw(X1, X2, mtype);
   - M = slmetric_pw(X1, X2, mtype, ...);

 $ Description $
    - M = slmetric_pw(X1, X2, mtype) Computes the metrics between
    column vectors of X1 and X2 pairwisely, using the metric
    specified by mtype. If X1 has n1 columns, X2 has n2 columns, then
    the resultant M would be of size n1 x n2. The entry at i-th row
    and j-th column of M represents the metric between X1(:, i) and
    X2(:, j).

    - M = slmetric_pw(X1, X2, mtype, ...) Some metric types requires
    extra parameters, which should be specified in params.

    - The supported metrics of this function are listed as follows:
      \*
      \t  Table 1. The supported metrics                             \\
      \h     name     &       description                            \\
          'eucdist'   &  Euclidean distance: ||x - y||               \\         
          'sqdist'    &  Square of Euclidean distance: ||x - y||^2   \\
          'dotprod'   &  Canonical dot product: <x,y> = x^T * y      \\
          'nrmcorr'   &  Normalized correlation (cosine angle):
                         (x^T * y ) / (||x|| * ||y||)                \\
          'angle'     &  Angle between two vectors (in radian)       \\
          'quadfrm'   &  Quadratic form:  x^T * Q * y                
                         Q is specified in the 1st extra parameter   \\
          'quaddiff'  &  Quadratic form of difference:
                         (x - y)^T * Q * (x - y),                
                         Q is specified in the 1st extra parameter   \\
          'cityblk'   &  City block distance (abssum of difference)  \\
          'maxdiff'   &  Maximum absolute difference                 \\
          'mindiff'   &  Minimum absolute difference                 \\
          'wsqdist'   &  Weighted square of Euclidean distance       \\
                         \sum_i w_i (x_i - y_i)^2,  w = (w_1, ..., w_d)                     
                         the weights w is specified in 1st extra parameter 
                         as a length-d column vector                  \\
      \*

 $ Remarks $
   - X1 and X2 are both matrices with n1 column vectors and n2 column 
     vectors respectively. Then the resultant matrix M will be a n1 * n2
     matrix. The entry at i-th row and j-th column of M is the metric between
     the i-th column vector in X1 and the j-th column vector in X2.

 $ History $
   - Created by Dahua Lin on Dec 06th, 2005
   - Modified by Dahua Lin on Apr 21st, 2005
       - regularize the error reporting
   - Modified by Dahua Lin on Sep 11st, 2005
       - completely rewrite the core codes based on new mex computation 
         cores, and the runtime efficiency in both time and space is 
         significantly increased.

CROSS-REFERENCE INFORMATION ^

This function calls:
  • sladdrowcols SLADDROWCOLS Adds the vectors to all rows and all columns
  • sldiff_pw SLDIFF_PW Measures the pair-wise difference
  • slmulrowcols SLMULROWCOLS Multiplies the vectors to all rows and all columns
  • slmulvec SLMULVEC multiplies a vector to columns or rows of a matrix
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
This function is called by:
  • slkmeans SLKMEANS Performs K-Means Clustering on samples
  • slclassify_eucnn SLCLASSIFY_EUCNN Classifies samples using Euclidena-based NN
  • slhistmetric_pw SLHISTMETRIC_PW Computes distance metrics between histograms pairwisely
  • slvechist SLVECHIST Makes the histogram on prototype vectors by voting
  • slfindnn SLFINDNN Finds the nearest neighbors using specified strategy
  • slpwmetricgraph SLPWMETRICGRAPH Constructs a graph based on pairwise metrics
  • slkernel SLKERNEL Computes the kernel for samples
  • slgaussmdist SLGAUSSMDIST Computes the Malanobis distance between samples and centers
  • slscatter SLSCATTER Compute the scatter matrix

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function M = slmetric_pw(X1, X2, mtype, varargin)
0002 %SLMETRIC_PW Compute the metric between column vectors pairwisely
0003 %
0004 % $ Syntax $
0005 %   - M = slmetric_pw(X1, X2, mtype);
0006 %   - M = slmetric_pw(X1, X2, mtype, ...);
0007 %
0008 % $ Description $
0009 %    - M = slmetric_pw(X1, X2, mtype) Computes the metrics between
0010 %    column vectors of X1 and X2 pairwisely, using the metric
0011 %    specified by mtype. If X1 has n1 columns, X2 has n2 columns, then
0012 %    the resultant M would be of size n1 x n2. The entry at i-th row
0013 %    and j-th column of M represents the metric between X1(:, i) and
0014 %    X2(:, j).
0015 %
0016 %    - M = slmetric_pw(X1, X2, mtype, ...) Some metric types requires
0017 %    extra parameters, which should be specified in params.
0018 %
0019 %    - The supported metrics of this function are listed as follows:
0020 %      \*
0021 %      \t  Table 1. The supported metrics                             \\
0022 %      \h     name     &       description                            \\
0023 %          'eucdist'   &  Euclidean distance: ||x - y||               \\
0024 %          'sqdist'    &  Square of Euclidean distance: ||x - y||^2   \\
0025 %          'dotprod'   &  Canonical dot product: <x,y> = x^T * y      \\
0026 %          'nrmcorr'   &  Normalized correlation (cosine angle):
0027 %                         (x^T * y ) / (||x|| * ||y||)                \\
0028 %          'angle'     &  Angle between two vectors (in radian)       \\
0029 %          'quadfrm'   &  Quadratic form:  x^T * Q * y
0030 %                         Q is specified in the 1st extra parameter   \\
0031 %          'quaddiff'  &  Quadratic form of difference:
0032 %                         (x - y)^T * Q * (x - y),
0033 %                         Q is specified in the 1st extra parameter   \\
0034 %          'cityblk'   &  City block distance (abssum of difference)  \\
0035 %          'maxdiff'   &  Maximum absolute difference                 \\
0036 %          'mindiff'   &  Minimum absolute difference                 \\
0037 %          'wsqdist'   &  Weighted square of Euclidean distance       \\
0038 %                         \sum_i w_i (x_i - y_i)^2,  w = (w_1, ..., w_d)
0039 %                         the weights w is specified in 1st extra parameter
0040 %                         as a length-d column vector                  \\
0041 %      \*
0042 %
0043 % $ Remarks $
0044 %   - X1 and X2 are both matrices with n1 column vectors and n2 column
0045 %     vectors respectively. Then the resultant matrix M will be a n1 * n2
0046 %     matrix. The entry at i-th row and j-th column of M is the metric between
0047 %     the i-th column vector in X1 and the j-th column vector in X2.
0048 %
0049 % $ History $
0050 %   - Created by Dahua Lin on Dec 06th, 2005
0051 %   - Modified by Dahua Lin on Apr 21st, 2005
0052 %       - regularize the error reporting
0053 %   - Modified by Dahua Lin on Sep 11st, 2005
0054 %       - completely rewrite the core codes based on new mex computation
0055 %         cores, and the runtime efficiency in both time and space is
0056 %         significantly increased.
0057 %
0058 
0059 
0060 %% parse and verify input arguments
0061 if nargin < 3
0062     raise_lackinput('slmetric_pw', 3);
0063 end
0064 mtype = lower(mtype);
0065 
0066 %% compute
0067 switch mtype        
0068     case {'eucdist', 'sqdist'}
0069         checkdim(X1, X2);
0070         sqs1 = sum(X1 .* X1, 1)';
0071         sqs2 = sum(X2 .* X2, 1);
0072         M = (-2) * X1' * X2;
0073         M = sladdrowcols(M, sqs2, sqs1);
0074         M(M < 0) = 0;                        
0075         if strcmp(mtype, 'eucdist')
0076             M = sqrt(M);
0077         end 
0078         
0079     case 'dotprod'
0080         checkdim(X1, X2);
0081         M = X1' * X2;
0082                 
0083     case {'nrmcorr', 'angle'}
0084         checkdim(X1, X2);
0085         M = X1' * X2;
0086         f1 = sqrt(sum(X1 .* X1, 1))';
0087         f2 = sqrt(sum(X2 .* X2, 1));
0088         f1(f1 < eps) = eps;  % prevent from being zeros
0089         f2(f2 < eps) = eps;
0090         f1 = 1 ./ f1;
0091         f2 = 1 ./ f2;
0092         M = slmulrowcols(M, f2, f1);        
0093         if strcmp(mtype, 'angle0')
0094             M = real(acos(M));
0095         end 
0096         
0097     case 'quadfrm'
0098         % parse parameters
0099         Q = varargin{1};
0100         [d1, d2] = size(Q);
0101         if size(X1, 1) ~= d1 || size(X2, 1) ~= d2
0102             error('sltoolbox:sizmismatch', ...
0103                 'The dimensions of X1 and X2 are not consistent with Q');
0104         end
0105         
0106         % compute
0107         M = X1' * Q * X2;
0108         
0109     case 'quaddiff'
0110         % parse parameters
0111         d = checkdim(X1, X2);
0112         Q = varargin{1};
0113         if ~isequal(size(Q), [d, d])
0114             error('sltoolbox:dimmismatch', ...
0115                 'The dimensions of X1 and X2 are not consistent with Q');
0116         end
0117         
0118         % compute
0119         qs1 = sum(X1 .* (Q * X1), 1)';
0120         qs2 = sum(X2 .* (Q * X2), 1);
0121         M = X1' * (-(Q + Q')) * X2;        
0122         M = sladdrowcols(M, qs2, qs1);
0123                 
0124     case 'cityblk'
0125         M = sldiff_pw(X1, X2, 'abssum');
0126         
0127     case 'maxdiff'
0128         M = sldiff_pw(X1, X2, 'maxdiff');
0129         
0130     case 'mindiff'
0131         M = sldiff_pw(X1, X2, 'mindiff');
0132                 
0133     case 'wsqdist'
0134         % parse parameters
0135         d = checkdim(X1, X2);
0136         w = varargin{1};
0137         if ~isequal(size(w), [d, 1])
0138             error('sltoolbox:sizmismatch', ...
0139                 'w is not a proper size column vector');
0140         end
0141 
0142         % compute
0143        wX1 = slmulvec(X1, w, 1);
0144        qs1 = sum(wX1 .* X1, 1)';
0145        clear wX1;
0146        wX2 = slmulvec(X2, w, 1);
0147        qs2 = sum(wX2 .* X2, 1);
0148        M = (-2) * X1' * wX2;
0149        clear wX2;
0150        M = sladdrowcols(M, qs2, qs1);
0151         
0152     otherwise
0153         error('sltoolbox:invalid_type', 'Unknown metric type %s', mtype);
0154         
0155 end
0156         
0157 %% Auxiliary function
0158 
0159 function d = checkdim(X1, X2)
0160 
0161 d = size(X1, 1);
0162 if d ~= size(X2, 1)
0163     error('sltoolbox:sizmismatch', ...
0164         'X1 and X2 have different sample dimensions');
0165 end
0166 
0167 
0168 
0169 
0170

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com