MATLAB Answers

How created a graph from unordered node list?

5 views (last 30 days)
I want to created a graph from an unordered list of nodes. However, Matlab creat graph from ordered node numbers. For example, My node list is as :
Now this would be the result if I plot the graph from this source and target nodes:
The results shows the range of nodes from 1 to 8; however, These nodes don't exist in this network. This is problematic since I have a huge road network like this (34000 unordered pairs of source and targets) and each node has and coordiantion. This seroiusly cause problem while I'm trying to plot it with coordiantes. I tried to remove those nodes but it completly changes the graph (I have no Idea why this happening). Thank you in advance for any advice or help.


Sign in to comment.

Accepted Answer

Christine Tobler
Christine Tobler on 6 Apr 2020
When a graph or digraph is specified using numbers, the assumption is that all nodes of this graph have numbers from 1:numberOfNodes. This is not the case when a graph or digraph is constructed from strings, so the simplest workaround is
G = graph(string(source), string(Target));


Mohammad Shapouri
Mohammad Shapouri on 6 Apr 2020
I did try this way but it's problematic when I'm trying to plot it based on node coordinates because the node order is not the same as numbers when you change numbers to string. I'm trying to change the coordiantion order based on string order. I hope it works. However, I believe this assumption should change because when you create a graph based on edge list, actually you specify the node numbers.
Mohammad Shapouri
Mohammad Shapouri on 6 Apr 2020
It worked after I change the arrangement of coordiantes based on string format. Thank you.

Sign in to comment.

More Answers (1)

darova on 6 Apr 2020
Try this
x = x(:);
y = y(:);
plot([x(source) x(target)],[y(source) y(tagrget)])
or patch function
fv.vertices = [x(:) y(:)];
fv.faces = [source(:) target(:)]];


Show 2 older comments
Mohammad Shapouri
Mohammad Shapouri on 6 Apr 2020
Unfortuantely, I can not attach the data (I'm not allowed) but I try to expalin more about it. This graph has 23297 nodes and 34586 edges. I have another list of coordinates (23297*3). The first column is node (sorted), the second is X and thrid is Y. The source and target list has 34586 rows each. However, While I create the graph from this list, the node numbers is 32308 because Matlab assume this graph has nodes from 1 to the last node and the coordination matrix only has X and Y for 23297 nodes. That is problmatic. I tried to fill this gap with imagenary X and Y for created nodes in Matlab (plot works perfect) but it has impact on graph analysis since it created near 9000 nodes and they really don't exist in the main network.
darova on 6 Apr 2020
Can you attach small part of the data or simplified example?
Mohammad Shapouri
Mohammad Shapouri on 6 Apr 2020
The problem solved by using string format. Thank you.

Sign in to comment.


Translated by