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

slmakeadjmat

PURPOSE ^

SLMAKEADJMAT Makes an adjacency matrix using edges and corresponing values

SYNOPSIS ^

function A = slmakeadjmat(n, nt, edges, vals, islogic, isspar)

DESCRIPTION ^

SLMAKEADJMAT Makes an adjacency matrix using edges and corresponing values

 $ Syntax $
   - A = slmakeadjmat(n, nt, edges, vals, islogic, issparse)

 $ Arguments $
   - n:        The number of (source) nodes
   - nt:       The number of (target) nodes
   - edges:    The set of edges 
   - vals:     The values associated with edges
   - islogic:  whether to make a logical matrix
   - isspar:   whether to make a sparse matrix
   - A:        The constructed adjacency matrix

 $ Description $
   - A = slmakeadjmat(n, nt, edges, vals, islogic, isspar) makes an 
     adjacency matrix using edges and corresponing values. Here, edges 
     and vals have the following configurations, and the value type of 
     the output matrix is determined by the configuration and the type 
     of vals.
       1. edges has 1 or 2 columns, vals is empty: a matrix with 
          all elements corresponding to existent edges set to 1.
       2. edges has 3 columns, vals is empty: a matrix with 
          all elements set using the 3rd column of edges.
       3. edges has 1 or 2 or 3 columns, vals has 1 column: a matrix 
          the values in vals are set to the matrix. The value column 
          in edges is ignored.
     When edges has 1 column, it contains the linear index of the edge 
     elements, when it has 2 columns, it contains the I and J subscripts
     of the edge elements, when it has 3 columns, it contains the I and J
     subscripts and the values.
  
 $ Remarks $
   - It is an internal support function, no checking will be performed.

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

CROSS-REFERENCE INFORMATION ^

This function calls:
This function is called by:
  • sledges2adjmat SLEDGES2ADJMAT Creates an adjacency matrix from edge set
  • slnngraph SLNNGRAPH Constructs a nearest neighborhood based graph
  • slpwgraph SLVALGRAPH Constructs a graph by computing values between nodes pairwisely
  • slsymgraph SLSYMGRAPH Forces symmetry of the adjacency matrix of a graph

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function A = slmakeadjmat(n, nt, edges, vals, islogic, isspar)
0002 %SLMAKEADJMAT Makes an adjacency matrix using edges and corresponing values
0003 %
0004 % $ Syntax $
0005 %   - A = slmakeadjmat(n, nt, edges, vals, islogic, issparse)
0006 %
0007 % $ Arguments $
0008 %   - n:        The number of (source) nodes
0009 %   - nt:       The number of (target) nodes
0010 %   - edges:    The set of edges
0011 %   - vals:     The values associated with edges
0012 %   - islogic:  whether to make a logical matrix
0013 %   - isspar:   whether to make a sparse matrix
0014 %   - A:        The constructed adjacency matrix
0015 %
0016 % $ Description $
0017 %   - A = slmakeadjmat(n, nt, edges, vals, islogic, isspar) makes an
0018 %     adjacency matrix using edges and corresponing values. Here, edges
0019 %     and vals have the following configurations, and the value type of
0020 %     the output matrix is determined by the configuration and the type
0021 %     of vals.
0022 %       1. edges has 1 or 2 columns, vals is empty: a matrix with
0023 %          all elements corresponding to existent edges set to 1.
0024 %       2. edges has 3 columns, vals is empty: a matrix with
0025 %          all elements set using the 3rd column of edges.
0026 %       3. edges has 1 or 2 or 3 columns, vals has 1 column: a matrix
0027 %          the values in vals are set to the matrix. The value column
0028 %          in edges is ignored.
0029 %     When edges has 1 column, it contains the linear index of the edge
0030 %     elements, when it has 2 columns, it contains the I and J subscripts
0031 %     of the edge elements, when it has 3 columns, it contains the I and J
0032 %     subscripts and the values.
0033 %
0034 % $ Remarks $
0035 %   - It is an internal support function, no checking will be performed.
0036 %
0037 % $ History $
0038 %   - Created by Dahua Lin, on Sep 9, 2006
0039 %
0040 
0041 %% Main skeleton
0042 
0043 if isempty(edges)
0044     A = make_emptymat(n, nt, 0, islogic, isspar);        
0045 else
0046     [ne, ncols] = size(edges);
0047     
0048     % decide indices
0049     inds = [];
0050     if isspar
0051         if ncols == 1
0052             inds = edges;
0053         else
0054             I = edges(:,1);
0055             J = edges(:,2);
0056         end
0057     else
0058         if ncols == 1
0059             inds = edges;
0060         else
0061             inds = sub2ind([n, nt], edges(:,1), edges(:,2));
0062         end
0063     end
0064     
0065     % decide values
0066     if isempty(vals)
0067         if ncols == 1 || ncols == 2
0068             if islogic
0069                 vals = true;
0070             else
0071                 vals = 1;
0072             end
0073         else
0074             if islogic
0075                 vals = (edges(:,3) ~= 0);
0076             else
0077                 vals = edges(:,3);
0078             end
0079         end
0080     else
0081         if islogic
0082             if ~islogical(vals)
0083                 vals = (vals ~= 0);
0084             end
0085         else
0086             if ~isa(vals, 'double')
0087                 vals = double(vals);
0088             end
0089         end
0090     end
0091                 
0092     
0093     % do construction
0094     if isempty(inds)    % use I and J to construct sparse matrix
0095         A = sparse(I, J, vals, n, nt);
0096     else                % create empty matrix first then use linear indices to fill
0097         A = make_emptymat(n, nt, ne, islogic, isspar);
0098         A(inds) = vals;
0099     end                                          
0100     
0101 end
0102 
0103 
0104 %% Auxiliary functions
0105 
0106 function A = make_emptymat(n, nt, ne, islogic, isspar)
0107 
0108 if isspar
0109     if islogic
0110         if ne > 0
0111             A = sparse(1, 1, false, n, nt, ne);
0112         else
0113             A = sparse(1, 1, false, n, nt);
0114         end
0115     else
0116         A = spalloc(n, nt, ne);
0117     end
0118 else
0119     if islogic
0120         A = false(n, nt);
0121     else
0122         A = zeros(n, nt);
0123     end
0124 end
0125             
0126 
0127 
0128     
0129 
0130 
0131

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

Contact us at files@mathworks.com