Error when running G= graph(s,t) in matlab
17 views (last 30 days)
Show older comments
I want to calculate L = laplacian(G) from a graph dataset. I imported the dataset, from here , which contains two columns as shown below:
# 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
To do so, I need to find G first so I use G = graph(FromNodeId, ToNodeId). When I did that, I got this error:
>> 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(...
I don't know why! Can I get a solution of that? Thank you.
2 Comments
Accepted Answer
More Answers (1)
lol
on 10 Nov 2021
THIS IS MY CODE:
filename = 'TABLA_AF1';
s = xlsread(filename,'A2:A25');
t = xlsread(filename,'B2:B25');
weights = xlsread(filename,'C2:C25');
G = graph(s,t,weights);
p = plot(G,'EdgeLabel',G.Edges.Weight);
[T,pred] = minspantree(G);
highlight(p,T)
AND THIS IS MY BASE DATA:
Nodo Inicial Nodo Final Distancia
A B 2
A E 16
A C 3
C F 3
C I 12
F E 7
F I 9
F H 6
I H 14
E H 2
E G 4
B E 8
B D 11
D E 2
D G 5
D J 7
G J 1
G L 6
H G 3
J L 12
H L 17
I K 7
H K 9
K L 17
AND I GET THIS ERROR:
Error using matlab.internal.graph.constructFromEdgeList (line 249)
Weight must have as many elements as edge list or be a scalar.
Can you tell me why please?
12 Comments
Walter Roberson
on 10 Nov 2021
filename = 'TABLA_AF1.xlsx';
[~, s] = xlsread(filename,'A2:A25');
[~, t] = xlsread(filename,'B2:B25');
weights = xlsread(filename,'C2:C25');
G = graph(s,t,weights);
p = plot(G,'EdgeLabel',G.Edges.Weight);
[T,pred] = minspantree(G);
highlight(p,T)
When you use xlsread() then the first output is always purely numeric. But you are trying to read a text column for s and t, and the numeric interpretation of those is all NaN. It happens that xlsread() trims out leading and trailing NaN columns and rows, so the column of NaN gets trimmed away entirely leaving you with empty entries for s and t.
My adjusted code here uses the second output instead, which is the text entries.
You would be better of using readtable(), which is able to automatically detect the data type of inputs.
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!