Ordering edge names in Digraph

4 次查看(过去 30 天)
I have this simple code just to make the question easy
s = [1 1 1 1 2 2 3 1 1];
t = [1 1 2 4 3 4 4 1 1];
G = digraph(s,t);
G.Nodes.Name = {'First', 'Second', 'Third' ,'Fourth'}';
G.Edges.Power = {'loop1' 'loop2' 'Hi' 'Hello' 'Bonjour' 'Welcome' 'Salut' 'loop3' 'loop4'}';
figure;
p = plot(G,'EdgeLabel',G.Edges.Power);
And this gives me the following graph.
My problem is I receive the the order of the source and destination vectors in a similar way to the given above, where I can not control the selfloops of (1,1). Some of them comes at the begining, some in the middel and some at the end. I want to give the same order of Edges.Power to the same order of the edges are defined by s,t. In other words, all selfloops around vertix 1(First) should be 'loop1' , 'loop2', 'loop3', loop4'.
Any help will be highly appreciated and thanks in advance.

采纳的回答

Christine Tobler
Christine Tobler 2020-9-8
The edges are reordered in the graph constructor, but it looks like the Power variable you're assigning is in the same order as the s, t vectors being passed in. You can pass the Power vector into the constructor, this way it will be reordered together with s and t:
s = [1 1 1 1 2 2 3 1 1];
t = [1 1 2 4 3 4 4 1 1];
nodeNames = {'First', 'Second', 'Third' ,'Fourth'}';
Power = {'loop1' 'loop2' 'Hi' 'Hello' 'Bonjour' 'Welcome' 'Salut' 'loop3' 'loop4'}';
G = digraph(s, t, table(Power), nodeNames);
plot(G, 'EdgeLabel', G.Edges.Power)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by