MATLAB Answers

How to add an edge between two nodes of two different graphs?

조회 수: 11(최근 30일)
Asaf McRock
Asaf McRock 2021년 1월 15일
편집: Asaf McRock 2021년 2월 3일
I'm trying to connect the nodes of graph g (Blue) with the nodes of graph h (Red).
so=[1 1 1 2 2 2 2 3 3 3 5];
ta=[2 3 4 3 4 5 6 6 7 5 7];
g=graph(so,ta);
p=plot(g);
x=p.XData;
y=p.YData;
z2=[0 0 0 0 0 0 0];
subplot(2,2,1);plot(g,'XData',x,'YData',y,'ZData',z2)
s=[1 1 1 2 2 3 3 3 3 4 4 5 5 ];
t=[2 5 6 3 4 7 4 5 6 7 5 7 6 ];
h=graph(s,t);
z=[-3 -3 -3 -3 -3 -3 -3];
hold on; subplot(2,2,1);plot(h,'XData',x,'YData',y,'ZData',z)
Is it something doable?
Thank you all for your continuous support!

채택된 답변

Christine Tobler
Christine Tobler 2021년 1월 18일
You could also add the edges of both graphs to one larger graph, and then use addedge to connect them. You would probably not get a clear separation visually of what used to be the first graph and the second graph this way, though.
so=[1 1 1 2 2 2 2 3 3 3 5];
ta=[2 3 4 3 4 5 6 6 7 5 7];
g=graph(so,ta);
p=plot(g);
x=p.XData;
y=p.YData;
z2=[0 0 0 0 0 0 0];
s=[1 1 1 2 2 3 3 3 3 4 4 5 5 ];
t=[2 5 6 3 4 7 4 5 6 7 5 7 6 ];
h=graph(s,t);
z=[-3 -3 -3 -3 -3 -3 -3];
gh = graph([so, s+7], [ta, t+7])
gh = addedge(gh, 2, 4+7)
plot(gh,'XData',[x, x],'YData',[y, y],'ZData',[z2, z2-3])
  댓글 수: 6
Asaf McRock
Asaf McRock 2021년 2월 3일
OMG! I wonder when can I reach this level of creativity.
Very interesting small examples indeed. I will start practicing and learning from here.
Thanks for your time and continuous support!

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

추가 답변(1개)

Asaf McRock
Asaf McRock 2021년 1월 16일
v1=[x(1) x(1)];
v2=[y(1) y(1)];
v3=[z2(1) z(1)];
j1=[x(2) x(4)];
j2=[y(2) y(4)];
j3=[z2(2) z(4)];
line(v1,v2,v3,'Color','red','LineStyle','--')
line(j1,j2,j3,'Color','red','LineStyle','--')
  댓글 수: 1
Asaf McRock
Asaf McRock 2021년 1월 16일
It's not possible to add an edge between nodes of different graphs. So, this is the only way, I guess, to visualize an interlink between nodes of different graph. It might be inefficient though especially for larger number of nodes.

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

Community Treasure Hunt

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

Start Hunting!

Translated by