creating a graph with nodes and edges

8 次查看(过去 30 天)
I've the following
tail = [2 3 4]
head = [3 4 7]
G = digraph(tail,head)
plot(G)
The node numbering isn't continuous here. When I plot, I find nodes 1, 5 and 6 created. How can I avoid this?
And how can I label nodes in such cases. I want node numbers as node labels.
When the numbering is contiguous I do,
G.Nodes.Name = cellstr(string(1:height(G.Nodes))');

采纳的回答

Steven Lord
Steven Lord 2020-8-29
If you pass in numbers as the sources and targets they are treated as node indices, and if you have a node with index 5 that means there must be nodes with indices 1, 2, 3, and 4.
You can pass the sources and targets as string arrays and they will then be treated as node names.
tail = [2 3 4]
head = [3 4 7]
G = digraph(string(tail), string(head))
h = plot(G)
highlight(h, ["3", "4"], 'EdgeColor', 'r')
Note that in this case node 2 is not node "2". The following command changes the line style of the edge between the second and third nodes in the Nodes table in G, which is the edge between "3" and "4" not the edge between "2" and "3".
highlight(h, [2 3], 'LineStyle', '--')

更多回答(0 个)

类别

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

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by