creating a graph with nodes and edges

2 ビュー (過去 30 日間)
Deepa Maheshvare
Deepa Maheshvare 2020 年 8 月 29 日
回答済み: Steven Lord 2020 年 8 月 29 日
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))');
  1 件のコメント
Deepa Maheshvare
Deepa Maheshvare 2020 年 8 月 29 日
Any suggestions?

サインインしてコメントする。

採用された回答

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 Center および File ExchangeGraph 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