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 Commenti

how did you import the Data and construct the node id vectors?
I used (import data) from Matlab itself and chose (column vectors)

Accedi per commentare.

 Risposta accettata

Steven Lord
Steven Lord il 2 Apr 2017
Indices in MATLAB start at 1, not 0. Try adding 1 to your index vectors.

Più risposte (1)

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 Commenti

What is reported for size(s), size(t), size(weights)? Also what is class() of each of those?
how can I check those parameters?
at the command prompt
size(s), size(t), size(weights)
class(s), class(t), class(weights)
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);
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)
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.
>> 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.
Thank you so much! it already works!

Accedi per commentare.

Categorie

Scopri di più su Graph and Network Algorithms in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by