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 slaffinitymat
Home > sltoolbox > graph > slaffinitymat.m

slaffinitymat

PURPOSE ^

SLAFFINITYMAT Constructs an affinity matrix

SYNOPSIS ^

function A = slaffinitymat(X, X2, nnparams, varargin)

DESCRIPTION ^

SLAFFINITYMAT Constructs an affinity matrix

 $ Syntax $
   - A = slaffinitymat(X, [], nnparams, ...)
   - A = slaffinitymat(X, X2, nnparams, ...)

 $ Arguments $
   - X:        The sample matrix of the (source) nodes
   - X2:       The sample matrix of the (target) nodes
   - nnparams: The cell array of parameters for finding nearest neighbors
               in the form of {method, ...}. Please refer to slfindnn
               for details of specifying nnparams.
   - A:        The constructed affinity matrix
   
 $ Description $
   - A = slaffinitymat(X, X2, nnparams, ...) constructs an affinity 
     matrix to represent the pairwise affinity between neighboring 
     samples. The affinity between non-neighboring samples is set to
     zero. You can indicate a self-affinity matrix (affinity among the
     the set of samples) by setting X2 to []. 
     By default, the function uses heated kernel to compute the affinity
     as explained below. In addition, you can use other formulas to 
     translate the distances to affinity by supplying your tfunctor in 
     the properties. 
     The following is a table of properties that you can specify:
       \*
       \t   The Properties of Affinity Matrix construction         \\
       \h     name         &      description                      \\
             'sparse'      & Whether to construct sparse matrix 
                             (default = true)                      \\
             'kernel'      & The kernel to compute affinity         \\
                             - 'heat': the heated kernel (default) 
                               it uses the following formula to translate
                               the Euclidean distances into affinities:
                                 a = exp(- d^2 / (2 *sigma^2))
                               Here sigma^2 is set to 
                                 diffusion^2 * avg(d^2). 
                               you can set the value of diffusion in the 
                               properties.
                             - 'simple':  the simple kernel
                               just set the affinity of all neighboring
                               samples to 1.
             'diffusion'   & The diffusion distance relative to 
                             the average distance. (default = 1)    \\
             'tfunctor'    & The function to transform the distance
                             values to edge values. (default = [])  \\
             'sym'         & whether to symmetrizes the graph 
                             (default = true)                       \\
             'symmethod'   & The method used to symmetrization       
                             (default = [])                          \\
             'excludeself' & Whether to exclude the edges connecting
                             the same node. (default = false).
       \*

 $ Arguments $
   - The properties sym, symmethod and excludeself only take effect
     when input X2 = [].

 $ Remarks $
   - It wrapps slnngraph to provide a convenient way to compute
     typical affinity matrix.

 $ History $
   - Created by Dahua Lin, on Sep 12nd, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slnngraph SLNNGRAPH Constructs a nearest neighborhood based graph
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
  • slparseprops SLPARSEPROPS Parses input parameters
This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function A = slaffinitymat(X, X2, nnparams, varargin)
0002 %SLAFFINITYMAT Constructs an affinity matrix
0003 %
0004 % $ Syntax $
0005 %   - A = slaffinitymat(X, [], nnparams, ...)
0006 %   - A = slaffinitymat(X, X2, nnparams, ...)
0007 %
0008 % $ Arguments $
0009 %   - X:        The sample matrix of the (source) nodes
0010 %   - X2:       The sample matrix of the (target) nodes
0011 %   - nnparams: The cell array of parameters for finding nearest neighbors
0012 %               in the form of {method, ...}. Please refer to slfindnn
0013 %               for details of specifying nnparams.
0014 %   - A:        The constructed affinity matrix
0015 %
0016 % $ Description $
0017 %   - A = slaffinitymat(X, X2, nnparams, ...) constructs an affinity
0018 %     matrix to represent the pairwise affinity between neighboring
0019 %     samples. The affinity between non-neighboring samples is set to
0020 %     zero. You can indicate a self-affinity matrix (affinity among the
0021 %     the set of samples) by setting X2 to [].
0022 %     By default, the function uses heated kernel to compute the affinity
0023 %     as explained below. In addition, you can use other formulas to
0024 %     translate the distances to affinity by supplying your tfunctor in
0025 %     the properties.
0026 %     The following is a table of properties that you can specify:
0027 %       \*
0028 %       \t   The Properties of Affinity Matrix construction         \\
0029 %       \h     name         &      description                      \\
0030 %             'sparse'      & Whether to construct sparse matrix
0031 %                             (default = true)                      \\
0032 %             'kernel'      & The kernel to compute affinity         \\
0033 %                             - 'heat': the heated kernel (default)
0034 %                               it uses the following formula to translate
0035 %                               the Euclidean distances into affinities:
0036 %                                 a = exp(- d^2 / (2 *sigma^2))
0037 %                               Here sigma^2 is set to
0038 %                                 diffusion^2 * avg(d^2).
0039 %                               you can set the value of diffusion in the
0040 %                               properties.
0041 %                             - 'simple':  the simple kernel
0042 %                               just set the affinity of all neighboring
0043 %                               samples to 1.
0044 %             'diffusion'   & The diffusion distance relative to
0045 %                             the average distance. (default = 1)    \\
0046 %             'tfunctor'    & The function to transform the distance
0047 %                             values to edge values. (default = [])  \\
0048 %             'sym'         & whether to symmetrizes the graph
0049 %                             (default = true)                       \\
0050 %             'symmethod'   & The method used to symmetrization
0051 %                             (default = [])                          \\
0052 %             'excludeself' & Whether to exclude the edges connecting
0053 %                             the same node. (default = false).
0054 %       \*
0055 %
0056 % $ Arguments $
0057 %   - The properties sym, symmethod and excludeself only take effect
0058 %     when input X2 = [].
0059 %
0060 % $ Remarks $
0061 %   - It wrapps slnngraph to provide a convenient way to compute
0062 %     typical affinity matrix.
0063 %
0064 % $ History $
0065 %   - Created by Dahua Lin, on Sep 12nd, 2006
0066 %
0067 
0068 %% parse input and prepare parameters
0069 
0070 if nargin < 3
0071     raise_lackinput('slaffinitymat', 3);
0072 end
0073 
0074 opts.sparse = true;
0075 opts.kernel = 'heat';
0076 opts.diffusion = 1;
0077 opts.tfunctor = [];
0078 opts.sym = true;
0079 opts.symmethod = [];
0080 opts.excludeself = false;
0081 opts = slparseprops(opts, varargin{:});
0082 
0083 if isempty(X2) 
0084     if opts.excludeself
0085         X2 = [];
0086     else
0087         X2 = X;
0088     end
0089 else
0090     opts.sym = false;
0091 end
0092 
0093 if isempty(opts.tfunctor)
0094     switch opts.kernel
0095         case 'heat'
0096             tfunctor = {@internal_compaffinity, opts.diffusion};
0097         case 'simple'
0098             tfunctor = @(x) ones(size(x));
0099         otherwise
0100             error('sltoolbox:invalidarg', ...
0101                 'Invalid kernel name: %s', opts.kernel);
0102     end
0103 else
0104     tfunctor = opts.tfunctor;
0105 end
0106 
0107 %% main wrapper
0108 
0109 A = slnngraph(X, X2, nnparams, ...
0110     'valtype', 'numeric', ...
0111     'sparse', opts.sparse, ...
0112     'tfunctor', tfunctor, ...
0113     'sym', opts.sym, ...
0114     'symmethod', opts.symmethod);
0115 
0116 %% The internal function to compute affinity
0117 
0118 function affvals = internal_compaffinity(dists, diffusion)
0119     
0120 sqs = dists .* dists;
0121 sqs = sqs(:);
0122 
0123 avgsq = sum(sqs) / length(sqs);
0124 s = (diffusion^2) * avgsq * 2;
0125 
0126 affvals = exp(-sqs / s);
0127 
0128 
0129 
0130 
0131 
0132 
0133 
0134 
0135 
0136 
0137 
0138 
0139

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

Contact us at files@mathworks.com