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
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!