Convert GML file to adjacency matrix in matlab

喜你入骨 提交于 2019-12-10 17:48:30

问题


I have a GML file of a directed graph (Political blogs). I want to use this graph in Matlab as an adjacency matrix. How can I convert it? Thanks.


回答1:


There is a sample code here for this purpose:

%Extracting edges from gml file graph
fileName = 'dolphins.gml';
inputfile = fopen(fileName);
A=[];
l=0;
k=1;
while 1
      % Get a line from the input file
      tline = fgetl(inputfile);
      % Quit if end of file
      if ~ischar(tline)
          break
      end
      nums = regexp(tline,'\d+','match');
      if length(nums)
          if l==1
              l=0;
              A(k,2)=str2num(nums{1});  
              k=k+1;
              continue;
          end
          A(k,1)=str2num(nums{1});
          l=1;
      else
          l=0;
          continue;
      end
end

A[], an [m x 2] matrix, contains the links between nodes.




回答2:


With R you can use:

library("multiplex") ## >v1.5 
read.gml(file, as="matrix")


来源:https://stackoverflow.com/questions/15918791/convert-gml-file-to-adjacency-matrix-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!