Home > Mateda2.0 > functions > generators > ReadFactorGraphFromData.m

# ReadFactorGraphFromData

## PURPOSE

[ListFactors] = ReadFactorGraphFromData(filename)

## SYNOPSIS

function[ListFactors] = ReadFactorGraphFromData(filename)

## DESCRIPTION

     [ListFactors] = ReadFactorGraphFromData(filename)
     ReadFactorGraphFromData:    Reads the  structure  of a given function from a file
     INPUTS
     filename:  file of name 'filename' with the following format:

        Total number of nodes
        Total number of variable  nodes
        for i=1 to Total number of variables nodes
          Variable node (for C compatibility i-1 instead of i is printed to the file)
          Cardinality of variable node i
        end
        Number of edges in the factor graph
        for i=1 to Total number of edges
          variable node incident to  edge i
          factor node incident to  edge i
        end
     OUTPUTS
     ListFactors: Each  cell  {i} stores the variables in the factor

     Last version 8/26/2008. Roberto Santana (roberto.santana@ehu.es)

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function[ListFactors] = ReadFactorGraphFromData(filename)
    0002 % [ListFactors] = ReadFactorGraphFromData(filename)
    0003 % ReadFactorGraphFromData:    Reads the  structure  of a given function from a file
    0004 % INPUTS
    0005 % filename:  file of name 'filename' with the following format:
    0006 %
    0007 %    Total number of nodes
    0008 %    Total number of variable  nodes
    0009 %    for i=1 to Total number of variables nodes
    0010 %      Variable node (for C compatibility i-1 instead of i is printed to the file)
    0011 %      Cardinality of variable node i
    0012 %    end
    0013 %    Number of edges in the factor graph
    0014 %    for i=1 to Total number of edges
    0015 %      variable node incident to  edge i
    0016 %      factor node incident to  edge i
    0017 %    end
    0018 % OUTPUTS
    0019 % ListFactors: Each  cell  {i} stores the variables in the factor
    0020 %
    0021 % Last version 8/26/2008. Roberto Santana (roberto.santana@ehu.es)
    0022
    0023 fid = fopen(filename,'r');
    0024
    0025 AllEntries = fscanf(fid,'%d');
    0026 fclose(fid);
    0027
    0028 NumberVar = AllEntries(2);  % Number of variable nodes
    0029
    0030
    0031 totfactors = AllEntries(1) - NumberVar; % Number of factor nodes
    0032
    0033 for i=1:totfactors,
    0034  ListFactors{i} = [];
    0035 end,
    0036
    0037 Card = AllEntries([4:2:2*NumberVar+2]);
    0038 firstfact = 2*NumberVar+3;
    0039 NumberEdges = AllEntries(firstfact);
    0040
    0041 edges = AllEntries(firstfact+1:end);
    0042
    0043 for i=1:2:size(edges,1)
    0044  aux = ListFactors{edges(i+1)-totfactors+1};
    0045  aux = [aux,edges(i)+1];
    0046  ListFactors{edges(i+1)-totfactors+1} = aux;
    0047 end
    0048
    0049