问题
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