How created a graph from unordered node list?

조회 수: 5 (최근 30일)
Mohammad Shapouri
Mohammad Shapouri 2020년 4월 6일
댓글: Mohammad Shapouri 2020년 4월 6일
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 :
source=[1,4,8,7];
Target=[7,8,1,4];
Now this would be the result if I plot the graph from this source and target nodes:
G=graph(source,Target);
P=plot(G);
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.

채택된 답변

Christine Tobler
Christine Tobler 2020년 4월 6일
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));
  댓글 수: 2
Mohammad Shapouri
Mohammad Shapouri 2020년 4월 6일
편집: Mohammad Shapouri 2020년 4월 6일
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 2020년 4월 6일
It worked after I change the arrangement of coordiantes based on string format. Thank you.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

darova
darova 2020년 4월 6일
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(:)]];
patch(fv)
  댓글 수: 5
darova
darova 2020년 4월 6일
Can you attach small part of the data or simplified example?
Mohammad Shapouri
Mohammad Shapouri 2020년 4월 6일
The problem solved by using string format. Thank you.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Networks에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by