【问题标题】:Error when running G= graph(s,t) in matlab在 matlab 中运行 G= graph(s,t) 时出错
【发布时间】:2017-04-01 15:18:01
【问题描述】:

我想从图表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(...

我不知道为什么!我可以得到解决方案吗?谢谢。

【问题讨论】:

  • fromNodeID 和 toNodeID 都是全双精度数组吗?试试FromNodeID=full(double(FromNodeID)); ToNodeID=full(double(ToNodeID));。 “源必须是节点索引的密集双数组。”在我看来,您可能正在输入稀疏数组或单一类型。
  • 问题在于记录数据的方式。数组是稀疏的,我已经检查过了。

标签: matlab graph complex-networks


【解决方案1】:

原来问题在于以这种方式使用 Graph 函数时不允许有零。 (见:Target must be a dense double array of node indices. How to Solve?

我下载了数据集并使用以下代码成功运行它。请注意,此代码使用系统命令,并不与所有操作系统兼容,但它应该足够简单,可以重写为您使用的任何操作系统。它还假定 .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 应该可以解决您的问题。换句话说,以下代码 sn -p 应该可以解决您的问题;如果不是,我建议您根据上面提供的代码重写。

G=graph(FromNodeIdFull+1,ToNodeIdFull+1);

将我的旧答案留在这里,因为删除它可能会导致其他人同时阅读此答案和它的 cmets 感到困惑。请注意,以下答案并未解决问题。

只是将我自己和NKN的cmets放入答案中:

问题在于数组是稀疏的,但graph() 似乎期望完整的数组。以下应该有效:

FromNodeIdFull=full(double(FromNodeId));
ToNodeIdFull=full(double(ToNodeId));
G=graph(FromNodeIdFull,ToNodeIdFull);

根据您的输入数组是否已经加倍,您可以从前两行中删除double()

【讨论】:

  • 非常感谢您的回复。我使用了你的解决方案,但我仍然得到同样的错误!!我不知道这个数据集有什么问题!!这让我绝望
  • 请不要接受不能解决您问题的答案;这可能会导致其他人忽略您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-11
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 2022-11-17
  • 2015-07-17
  • 2023-04-03
相关资源
最近更新 更多