在 matlab 中 运行 G= graph(s,t) 时出错
Error when running G= graph(s,t) in matlab
我想根据图表 dataset 计算 L = laplacian(G)
。我导入了包含两列的数据集:FromNodeId 和 ToNodeId:
# Nodes: 3997962 Edges: 34681189
# FromNodeId ToNodeId
0 1
0 2
0 31
0 73
0 80
0 113619
0 2468556
0 2823829
0 2823833
0 2846857
0 2947898
0 3011654
0 3701688
0 3849377
0 4036524
0 4036525
0 4036527
0 4036529
0 4036531
0 4036533
0 4036534
0 4036536
0 4036537
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
为此,我需要先找到 G
,所以我使用 G = graph(FromNodeId, FromNodeId)
。当我这样做时,我得到了这个错误:
>> G = graph(fromNodeId,toNodeId)
Error using matlab.internal.graph.MLGraph
Source must be a dense double array of node indices.
Error in matlab.internal.graph.constructFromEdgeList (line 125)
G = underlyingCtor(double(s), double(t), totalNodes);
Error in graph (line 264)
matlab.internal.graph.constructFromEdgeList(...
我不知道为什么!我能得到解决方案吗?谢谢。
原来问题在于以这种方式使用 Graph 函数时不允许使用零。 (参见:)
我下载了数据集,运行 使用以下代码成功下载了它。请注意,此代码使用系统命令,并不与所有操作系统兼容,但它应该足够简单,可以重写到您使用的任何操作系统。它还假定 .txt 文件位于工作目录中。
% Removes first lines with comments in them; this system command was tested on Linux Ubuntu 14.04 and is probably not portable to Windows.
% If this system command doesn't work, manually remove the first four lines from the text file.
system('tail -n +5 com-lj.ungraph.txt > delimitedFile.txt');
% Read the newly created delimited file and add 1 to all nodes.
edges=dlmread('delimitedFile.txt')+1;
% Build the graph
G=graph(edges(:,1),edges(:,2));
假设您已经像我那样构建了数组,将 1 添加到 FromNodeIdFull 和 ToNodeIdFull 应该可以解决您的问题。换句话说,下面的代码片段应该可以解决您的问题;如果不是,我建议您根据上面提供的代码重写。
G=graph(FromNodeIdFull+1,ToNodeIdFull+1);
在这里留下我的旧答案,因为删除它可能会给同时阅读此答案和评论的其他人造成混淆。请注意,以下答案并未解决问题。
只是将我自己和 NKN 的评论放入答案中:
问题在于数组是稀疏的,但 graph()
似乎需要完整的数组。以下应该有效:
FromNodeIdFull=full(double(FromNodeId));
ToNodeIdFull=full(double(ToNodeId));
G=graph(FromNodeIdFull,ToNodeIdFull);
根据您的输入数组是否已经是双精度数组,您可以从前两行中删除 double()
。
我想根据图表 dataset 计算 L = laplacian(G)
。我导入了包含两列的数据集:FromNodeId 和 ToNodeId:
# Nodes: 3997962 Edges: 34681189
# FromNodeId ToNodeId
0 1
0 2
0 31
0 73
0 80
0 113619
0 2468556
0 2823829
0 2823833
0 2846857
0 2947898
0 3011654
0 3701688
0 3849377
0 4036524
0 4036525
0 4036527
0 4036529
0 4036531
0 4036533
0 4036534
0 4036536
0 4036537
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
为此,我需要先找到 G
,所以我使用 G = graph(FromNodeId, FromNodeId)
。当我这样做时,我得到了这个错误:
>> G = graph(fromNodeId,toNodeId)
Error using matlab.internal.graph.MLGraph
Source must be a dense double array of node indices.
Error in matlab.internal.graph.constructFromEdgeList (line 125)
G = underlyingCtor(double(s), double(t), totalNodes);
Error in graph (line 264)
matlab.internal.graph.constructFromEdgeList(...
我不知道为什么!我能得到解决方案吗?谢谢。
原来问题在于以这种方式使用 Graph 函数时不允许使用零。 (参见:
我下载了数据集,运行 使用以下代码成功下载了它。请注意,此代码使用系统命令,并不与所有操作系统兼容,但它应该足够简单,可以重写到您使用的任何操作系统。它还假定 .txt 文件位于工作目录中。
% Removes first lines with comments in them; this system command was tested on Linux Ubuntu 14.04 and is probably not portable to Windows.
% If this system command doesn't work, manually remove the first four lines from the text file.
system('tail -n +5 com-lj.ungraph.txt > delimitedFile.txt');
% Read the newly created delimited file and add 1 to all nodes.
edges=dlmread('delimitedFile.txt')+1;
% Build the graph
G=graph(edges(:,1),edges(:,2));
假设您已经像我那样构建了数组,将 1 添加到 FromNodeIdFull 和 ToNodeIdFull 应该可以解决您的问题。换句话说,下面的代码片段应该可以解决您的问题;如果不是,我建议您根据上面提供的代码重写。
G=graph(FromNodeIdFull+1,ToNodeIdFull+1);
在这里留下我的旧答案,因为删除它可能会给同时阅读此答案和评论的其他人造成混淆。请注意,以下答案并未解决问题。
只是将我自己和 NKN 的评论放入答案中:
问题在于数组是稀疏的,但 graph()
似乎需要完整的数组。以下应该有效:
FromNodeIdFull=full(double(FromNodeId));
ToNodeIdFull=full(double(ToNodeId));
G=graph(FromNodeIdFull,ToNodeIdFull);
根据您的输入数组是否已经是双精度数组,您可以从前两行中删除 double()
。