参考:http://stackoverflow.com/questions/15918791/convert-gml-file-to-adjacency-matrix-in-matlab
内容如下的GML文件
node
[
id 4938
]
node
[
id 4939
]
node
[
id 4940
]
edge
[
source 8
target 6
]
edge
[
source 8
target 7
]
edge
[
source 9
target 8
]
edge
[
source 10
target 9
]
edge
[
source 13
target 5
就是一幅图,要把它处理成邻接矩阵,便于后面处理, 直接用正则表达式就可以了。
代码如下:
function G = importgml(fileName)
inputfile = fopen(fileName);
A=[];
l=0;
k=1;
ii = 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'); %get number from string
if ii == 3
nums
end
ii = ii+1;
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
G=[];
length(A)
for i=1:length(A)
G(A(i,1)+1,A(i,2)+1) = 1;
end