Error when running G= graph(s,t) in matlab

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 commentaires

Walter Roberson
Walter Roberson le 2 Avr 2017
how did you import the Data and construct the node id vectors?
Ahmad Aseeri
Ahmad Aseeri le 2 Avr 2017
I used (import data) from Matlab itself and chose (column vectors)

Connectez-vous pour commenter.

 Réponse acceptée

Steven Lord
Steven Lord le 2 Avr 2017

0 votes

Indices in MATLAB start at 1, not 0. Try adding 1 to your index vectors.

Plus de réponses (1)

lol
lol le 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 commentaires

Walter Roberson
Walter Roberson le 10 Nov 2021
What is reported for size(s), size(t), size(weights)? Also what is class() of each of those?
lol
lol le 10 Nov 2021
how can I check those parameters?
at the command prompt
size(s), size(t), size(weights)
class(s), class(t), class(weights)
lol
lol le 10 Nov 2021
I just get these:
weightsError using matlab.internal.graph.constructFromEdgeList (line 249)
Weight must have as many elements as edge list or be a scalar.
Error in graph (line 300)
matlab.internal.graph.constructFromEdgeList(...
Error in A01382540_AF1 (line 5)
G = graph(s,t,weights);
Walter Roberson
Walter Roberson le 10 Nov 2021
These are not commands intended to somehow directly make the code work. These are commands to execute at the command line after the error, and then you post what the results were for us to examine so that we can make better guesses about what the problem is.
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)
lol
lol le 10 Nov 2021
YES PLEASE, I NEED HELP
Execute this code:
filename = 'TABLA_AF1.xlsx';
s = xlsread(filename,'A2:A25');
t = xlsread(filename,'B2:B25');
weights = xlsread(filename,'C2:C25');
size(s), size(t), size(weights)
class(s), class(t), class(weights)
and tell us what the output is.
lol
lol le 10 Nov 2021
>> A01382540_AF1
ans =
0 0
ans =
0 0
ans =
24 1
ans =
'double'
ans =
'double'
ans =
'double'
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.
lol
lol le 10 Nov 2021
Thank you so much! it already works!

Connectez-vous pour commenter.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by