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

slpwgraph

PURPOSE ^

SLVALGRAPH Constructs a graph by computing values between nodes pairwisely

SYNOPSIS ^

function G = slpwgraph(Xs, Xt, n, nt, evalfunctor, varargin)

DESCRIPTION ^

SLVALGRAPH Constructs a graph by computing values between nodes pairwisely

 $ Syntax $
   - G = slpwgraph(X, Xt, n, nt, evalfunctor, ...)

 $ Arguments $
   - X:            The set of (source) nodes
   - Xt:           The set of (target) nodes
   - n:            The number of (source) nodes
   - nt:           The number of (target) nodes
   - evalfunctor:  The functor to evaluate the values between two sets of
                   nodes, it should be like the following form:
                   V = f(X, Xt, inds1, inds2, ...)
                   Here, inds1 and inds2 are the indices selected in the
                   subset for current batch of computation. If inds1 and
                   inds2 respectively refer to n1 and n2 samples, then
                   V should be a n1 x n2 matrix (full or sparse)
   - G:            The constructed graph

 $ Description $
   - G = slpwgraph(X, Xt, n, nt, evalfunctor, ...) constructs a adjacency 
     matrix of a graph by computing edge values between every pair of the 
     nodes. If Xt is empty, then Xt is considered as the same as X,
     nt is considered as equal to n.
     You can specify the following properties:
     \*
     \t   The Properties of Graph Matrix construction           \\
     \h      name     &      description
            'sparse'  & whether the target graph G is sparse 
                        (default = true)
            'valtype' & The type of values in G: 'logical'|'numeric'
                        (default = 'numeric')
                        The value output by evalfunctor should conform
                        to the specified valtype
            'maxblk'  & The maximum number of elements that can be
                        computed in each batch. (default = 1e7) 
     \*
  
 $ History $
   - Created by Dahua Lin, on Sep 8th, 2006
   - Modified by Dahua Lin, on Sep 10th, 2006
       - Use new graph construction functions
       - Add support for bigraph

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slmakeadjmat SLMAKEADJMAT Makes an adjacency matrix using edges and corresponing values
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
  • slequalpar2D SLEQUALPAR Partition a 2D array with balances for width and height
  • slevalfunctor SLEVALFUNCTOR Evaluates a functor
  • slparseprops SLPARSEPROPS Parses input parameters
This function is called by:
  • slpwmetricgraph SLPWMETRICGRAPH Constructs a graph based on pairwise metrics

SOURCE CODE ^

0001 function G = slpwgraph(Xs, Xt, n, nt, evalfunctor, varargin)
0002 %SLVALGRAPH Constructs a graph by computing values between nodes pairwisely
0003 %
0004 % $ Syntax $
0005 %   - G = slpwgraph(X, Xt, n, nt, evalfunctor, ...)
0006 %
0007 % $ Arguments $
0008 %   - X:            The set of (source) nodes
0009 %   - Xt:           The set of (target) nodes
0010 %   - n:            The number of (source) nodes
0011 %   - nt:           The number of (target) nodes
0012 %   - evalfunctor:  The functor to evaluate the values between two sets of
0013 %                   nodes, it should be like the following form:
0014 %                   V = f(X, Xt, inds1, inds2, ...)
0015 %                   Here, inds1 and inds2 are the indices selected in the
0016 %                   subset for current batch of computation. If inds1 and
0017 %                   inds2 respectively refer to n1 and n2 samples, then
0018 %                   V should be a n1 x n2 matrix (full or sparse)
0019 %   - G:            The constructed graph
0020 %
0021 % $ Description $
0022 %   - G = slpwgraph(X, Xt, n, nt, evalfunctor, ...) constructs a adjacency
0023 %     matrix of a graph by computing edge values between every pair of the
0024 %     nodes. If Xt is empty, then Xt is considered as the same as X,
0025 %     nt is considered as equal to n.
0026 %     You can specify the following properties:
0027 %     \*
0028 %     \t   The Properties of Graph Matrix construction           \\
0029 %     \h      name     &      description
0030 %            'sparse'  & whether the target graph G is sparse
0031 %                        (default = true)
0032 %            'valtype' & The type of values in G: 'logical'|'numeric'
0033 %                        (default = 'numeric')
0034 %                        The value output by evalfunctor should conform
0035 %                        to the specified valtype
0036 %            'maxblk'  & The maximum number of elements that can be
0037 %                        computed in each batch. (default = 1e7)
0038 %     \*
0039 %
0040 % $ History $
0041 %   - Created by Dahua Lin, on Sep 8th, 2006
0042 %   - Modified by Dahua Lin, on Sep 10th, 2006
0043 %       - Use new graph construction functions
0044 %       - Add support for bigraph
0045 %
0046 
0047 %% parse and verify input arguments
0048 
0049 if nargin < 5
0050     raise_lackinput('slpwgraph', 5);
0051 end
0052 
0053 if isempty(Xt)
0054     Xt = Xs;
0055     nt = n;
0056 end
0057 
0058 tarsiz = [n, nt];
0059 
0060 opts.sparse = true;
0061 opts.valtype = 'numeric';
0062 opts.maxblk = 1e7;
0063 opts = slparseprops(opts, varargin{:});
0064 
0065 if ~ismember(opts.valtype, {'logical', 'numeric'})
0066     error('sltoolbox:invalidarg', ...
0067         'Invalid value type for graph: %s', opts.valtype);
0068 end
0069 
0070 
0071 %% compute
0072 
0073 % create partitions
0074 
0075 ps = slequalpar2D(tarsiz, opts.maxblk);
0076 nm = length(ps(1).sinds);
0077 nn = length(ps(2).sinds);
0078 
0079 % compute
0080 
0081 if opts.sparse
0082     nblks = nm * nn;
0083     CI = cell(nblks, 1);
0084     CJ = cell(nblks, 1);
0085     CV = cell(nblks, 1);
0086     
0087     k = 0;    
0088     for i = 1 : nm
0089     for j = 1 : nn
0090         
0091         % get indices
0092         k = k + 1;
0093         inds1 = ps(1).sinds(i):ps(1).einds(i);
0094         inds2 = ps(2).sinds(j):ps(2).einds(j);
0095         
0096         % compute
0097         curV = slevalfunctor(evalfunctor, Xs, Xt, inds1, inds2);
0098         
0099         % filter
0100         [curI, curJ, curV] = find(curV);
0101         curI = curI + (inds1(1) - 1);
0102         curJ = curJ + (inds2(1) - 1);
0103         
0104         % store
0105         CI{k} = curI;
0106         CJ{k} = curJ;
0107         CV{k} = curV;
0108         
0109     end
0110     end
0111     
0112     CI = vertcat(CI{:});
0113     CJ = vertcat(CJ{:});
0114     CV = vertcat(CV{:});
0115     
0116     edges = [CI, CJ];
0117     clear CI CJ;
0118     
0119     islogic = strcmp(opts.valtype, 'logic');
0120     
0121     G = slmakeadjmat(n, nt, edges, CV, islogic, true);
0122                         
0123 else
0124     
0125     switch opts.valtype
0126         case 'logical'
0127             G = false(n, nt);
0128         case 'numeric'
0129             G = zeros(n, nt);
0130     end
0131     
0132     for i = 1 : nm
0133     for j = 1 : nn
0134         
0135         % get indices
0136         inds1 = ps(1).sinds(i):ps(1).einds(i);
0137         inds2 = ps(2).sinds(j):ps(2).einds(j);
0138         
0139         % compute
0140         curV = slevalfunctor(evalfunctor, Xs, Xt, inds1, inds2);
0141         
0142         % store
0143         G(inds1, inds2) = curV;                
0144         
0145     end
0146     end
0147         
0148 end
0149 
0150                 
0151 
0152 
0153 
0154 
0155 
0156 
0157

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

Contact us at files@mathworks.com