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

slpruneedgeset

PURPOSE ^

SLPRUNEEDGESET Prunes the edge set

SYNOPSIS ^

function edges = slpruneedgeset(n, nt, edges, method)

DESCRIPTION ^

SLPRUNEEDGESET Prunes the edge set

 $ Syntax $
   - edges = slpruneedgeset(n, nt, edges)
   - edges = slpruneedgeset(n, nt, edges, method)

 $ Arguments $
   - n:            The number of (source) nodes
   - nt:           The number of (target) nodes
   - edges:        The input/output edge set
   - method:       The method of merging.

 $ Description $
   - edges = slpruneedgeset(n, nt, edges) prunes the edge set using
     default method, so that the appositional edges (the edges from the 
     same source and to the same target) has only one entry in the 
     edge set. 

   - edges = slpruneedgeset(n, nt, edges, method) prunes the edge set 
     using the specified method. We have following methods:
       - 'nomulti':    report an error when multiple entries exist for
                       the same edge. (for ncols = 2 and 3)
       - 'noconflict': report an error when multiple entries exist for
                       the same edge having different values. That multiple
                       entries exist for the same edge with same values
                       is allowed. (for ncols = 2 and 3)
       - 'exist':      Despite the number of ocurrence, 
                       if ncols = 2, preserve all existent edges
                       if ncols = 3, set value to all existent edges to 1
       - 'noval':      preserve all existent edges, for both ncols = 2 
                       and 3, the value column is discarded. 
       - 'count':      Set the value to the number of occurrence of 
                       the edge. (for ncols = 2 and 3, however, the output
                       always have 3 columns, with the third column being
                       the count value of the corresponding edge)
       - 'sum':        Set the value to the sum of the values for the
                       edges (only for ncols = 3)
       - 'avg':        Set the value to the average of the values for 
                       the edges (only for ncols = 3)
       - 'max':        Set the value to the maximum value for the edges
                       (only for ncols = 3)
       - 'min':        Set the value to the minimum value for the edges
                       (only for ncols = 3)
       - 'first':      When multiple values exist for the same edge,
                       only the first one takes effects, others are 
                       ignored. (for ncols = 2 and 3)
       - 'last':       When multiple values exist for the same edge,
                       only the last one takes effects, others are 
                       ignored. (for ncols = 2 and 3)
     By default, when ncols == 2, the default method is 'noval', when
     ncols == 3, the default method is 'last'.
     The method can also be a function_handle using the following form:
       V = fp(V0, nums, sinds, einds)
       The input:
           - V0: the initial value (may be repeated) groupped by edges
           - nums: the numbers of values for edges
           - sinds: the start positions of the values for edges
           - einds: the end positions of the value for edges
       It should output V, a number of pruned edges x 1 column vector 
       with each value for a pruned edge.

 $ History $
   - Created by Dahua Lin, on Sep 9, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
  • slcount SLCOUNT Count the number of sum entities
  • slexpand SLEXPAND Expand a set to multiple instance
  • slignorevars SLIGNOREVARS Ignores the input variables
  • slnums2bounds SLNUMS2BOUNDS Compute the index-boundaries from section sizes
This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function edges = slpruneedgeset(n, nt, edges, method)
0002 %SLPRUNEEDGESET Prunes the edge set
0003 %
0004 % $ Syntax $
0005 %   - edges = slpruneedgeset(n, nt, edges)
0006 %   - edges = slpruneedgeset(n, nt, edges, method)
0007 %
0008 % $ Arguments $
0009 %   - n:            The number of (source) nodes
0010 %   - nt:           The number of (target) nodes
0011 %   - edges:        The input/output edge set
0012 %   - method:       The method of merging.
0013 %
0014 % $ Description $
0015 %   - edges = slpruneedgeset(n, nt, edges) prunes the edge set using
0016 %     default method, so that the appositional edges (the edges from the
0017 %     same source and to the same target) has only one entry in the
0018 %     edge set.
0019 %
0020 %   - edges = slpruneedgeset(n, nt, edges, method) prunes the edge set
0021 %     using the specified method. We have following methods:
0022 %       - 'nomulti':    report an error when multiple entries exist for
0023 %                       the same edge. (for ncols = 2 and 3)
0024 %       - 'noconflict': report an error when multiple entries exist for
0025 %                       the same edge having different values. That multiple
0026 %                       entries exist for the same edge with same values
0027 %                       is allowed. (for ncols = 2 and 3)
0028 %       - 'exist':      Despite the number of ocurrence,
0029 %                       if ncols = 2, preserve all existent edges
0030 %                       if ncols = 3, set value to all existent edges to 1
0031 %       - 'noval':      preserve all existent edges, for both ncols = 2
0032 %                       and 3, the value column is discarded.
0033 %       - 'count':      Set the value to the number of occurrence of
0034 %                       the edge. (for ncols = 2 and 3, however, the output
0035 %                       always have 3 columns, with the third column being
0036 %                       the count value of the corresponding edge)
0037 %       - 'sum':        Set the value to the sum of the values for the
0038 %                       edges (only for ncols = 3)
0039 %       - 'avg':        Set the value to the average of the values for
0040 %                       the edges (only for ncols = 3)
0041 %       - 'max':        Set the value to the maximum value for the edges
0042 %                       (only for ncols = 3)
0043 %       - 'min':        Set the value to the minimum value for the edges
0044 %                       (only for ncols = 3)
0045 %       - 'first':      When multiple values exist for the same edge,
0046 %                       only the first one takes effects, others are
0047 %                       ignored. (for ncols = 2 and 3)
0048 %       - 'last':       When multiple values exist for the same edge,
0049 %                       only the last one takes effects, others are
0050 %                       ignored. (for ncols = 2 and 3)
0051 %     By default, when ncols == 2, the default method is 'noval', when
0052 %     ncols == 3, the default method is 'last'.
0053 %     The method can also be a function_handle using the following form:
0054 %       V = fp(V0, nums, sinds, einds)
0055 %       The input:
0056 %           - V0: the initial value (may be repeated) groupped by edges
0057 %           - nums: the numbers of values for edges
0058 %           - sinds: the start positions of the values for edges
0059 %           - einds: the end positions of the value for edges
0060 %       It should output V, a number of pruned edges x 1 column vector
0061 %       with each value for a pruned edge.
0062 %
0063 % $ History $
0064 %   - Created by Dahua Lin, on Sep 9, 2006
0065 %
0066 
0067 %% parse and verify input
0068 
0069 if nargin < 3
0070     raise_lackinput('slpruneedgeset', 3);
0071 end    
0072 
0073 if isempty(edges)
0074     return;
0075 end
0076 
0077 ncols = size(edges, 2);
0078 if ~isnumeric(edges) || ndims(edges) ~= 2 || (ncols ~= 2 && ncols ~= 3)
0079     error('sltoolbox:invalidarg', ...
0080         'The edges should be an nedges x 2 or nedges x 3 matrix');
0081 end
0082 
0083 if nargin < 4 || isempty(method)
0084     if ncols == 2
0085         method = 'exist';
0086     else
0087         method = 'sum';
0088     end
0089 end
0090 
0091 %% delegate
0092 
0093 if ischar(method)
0094     switch method
0095         case 'nomulti'
0096             fp = @prune_nomulti;
0097             require_value = false;
0098         case 'noconflict'
0099             fp = @prune_noconflict;
0100             require_value = false;
0101         case 'exist'
0102             fp = @prune_exist;
0103             require_value = false;
0104         case 'noval'
0105             fp = @prune_noval;
0106             require_value = false;
0107         case 'count'
0108             fp = @prune_count;
0109             require_value = false;
0110         case 'sum'
0111             fp = @prune_sum;
0112             require_value = true;
0113         case 'avg'
0114             fp = @prune_avg;
0115             require_value = true;
0116         case 'max'
0117             fp = @prune_max;
0118             require_value = true;
0119         case 'min'
0120             fp = @prune_min;
0121             require_value = true;
0122         case 'first'
0123             fp = @prune_first;
0124             require_value = false;
0125         case 'last'
0126             fp = @prune_last;
0127             require_value = false;
0128         otherwise
0129             error('sltoolbox:invalidarg', ...
0130                 'Unknown edgeset prune method: %s', method);
0131     end
0132 elseif isa(method, 'function_handle')
0133     fp = method;
0134 else
0135     error('sltoolbox:invalidarg', ...
0136         'The prune method should be either a string or a function handle');
0137 end
0138 
0139 if ncols == 2 && require_value
0140     error('sltoolbox:rterror', ...
0141         'The prune method requires the value column in the edges');
0142 end
0143 
0144 %% arrange edges
0145 
0146 % get indices
0147 I = edges(:, 1);
0148 J = edges(:, 2);
0149 inds = sub2ind([n, nt], I, J);
0150 
0151 % get the sorting
0152 [inds, si] = sort(inds);
0153 nums = slcount(inds);
0154 clear inds;
0155 
0156 % sort I J V0
0157 I = I(si);
0158 J = J(si);
0159 if ncols == 3
0160     V0 = edges(si, 3);
0161 else
0162     V0 = [];
0163 end
0164 clear si;
0165 
0166 
0167 %% do prune on V0
0168 
0169 % prune the I J
0170 [sinds, einds] = slnums2bounds(nums);
0171 I = I(sinds);
0172 J = J(sinds);
0173 
0174 % prune the V
0175 V = fp(V0, nums, sinds, einds);
0176 
0177 %% merge pruned I J V
0178 
0179 if isempty(V)
0180     edges = [I, J];
0181 else
0182     edges = [I, J, V];
0183 end
0184 
0185 
0186 %% value prune functions
0187 
0188 function V = prune_nomulti(V0, nums, sinds, einds)
0189 
0190 slignorevars(sinds, einds);
0191 
0192 if any(nums > 1)
0193     error('sltoolbox:rterror', ...
0194         'It is not allowed that multiple entries for the same edge');
0195 end
0196 V = V0;
0197 
0198 
0199 function V = prune_noconflict(V0, nums, sinds, einds)
0200 
0201 slignorevars(einds);
0202 
0203 if ~isempty(V0)
0204     V = V0(sinds);
0205     if any(nums > 1)
0206         Vr = slexpand(nums, V);
0207         if ~isequal(V0, Vr)
0208             error('sltoolbox:rterror', ...
0209                 'There is confliction between values specified for the same edge');
0210         end
0211     end
0212 else
0213     V = [];
0214 end
0215 
0216 
0217 function V = prune_exist(V0, nums, sinds, einds)
0218 
0219 slignorevars(sinds, einds);
0220 
0221 if ~isempty(V0)
0222     nedges = length(nums);
0223     V = ones(nedges, 1);
0224 else
0225     V = [];
0226 end
0227 
0228 
0229 function V = prune_noval(V0, nums, sinds, einds)
0230 
0231 slignorevars(V0, nums, sinds, einds);
0232 V = [];
0233 
0234 
0235 function V = prune_count(V0, nums, sinds, einds)
0236 
0237 slignorevars(V0, sinds, einds);
0238 V = nums;
0239 
0240 
0241 function V = prune_sum(V0, nums, sinds, einds)
0242 
0243 ne = length(nums);
0244 V = zeros(ne, 1);
0245 for i = 1 : ne
0246     curV0 = V0(sinds(i):einds(i));
0247     V(i) = sum(curV0);
0248 end
0249 
0250 
0251 function V = prune_avg(V0, nums, sinds, einds)
0252 
0253 V = prune_sum(V0, nums, sinds, einds);
0254 V = V ./ nums;
0255 
0256 
0257 function V = prune_max(V0, nums, sinds, einds)
0258 
0259 ne = length(nums);
0260 V = zeros(ne, 1);
0261 for i = 1 : ne
0262     curV0 = V0(sinds(i):einds(i));
0263     V(i) = max(curV0);
0264 end 
0265 
0266 
0267 function V = prune_min(V0, nums, sinds, einds)
0268 
0269 ne = length(nums);
0270 V = zeros(ne, 1);
0271 for i = 1 : ne
0272     curV0 = V0(sinds(i):einds(i));
0273     V(i) = min(curV0);
0274 end 
0275 
0276 
0277 function V = prune_first(V0, nums, sinds, einds)
0278 
0279 slignorevars(V0, nums, einds);
0280 if ~isempty(V0)
0281     V = V0(sinds);
0282 else
0283     V = [];
0284 end
0285 
0286 
0287 function V = prune_last(V0, nums, sinds, einds)
0288 
0289 slignorevars(V0, nums, sinds);
0290 if ~isempty(V0)
0291     V = V0(einds);
0292 else
0293     V = [];
0294 end
0295 
0296

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

Contact us at files@mathworks.com