creating a graph with nodes and edges

1 vue (au cours des 30 derniers jours)
Deepa Maheshvare
Deepa Maheshvare le 29 Août 2020
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))');

Réponse acceptée

Steven Lord
Steven Lord le 29 Août 2020
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', '--')

Plus de réponses (0)

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by