creating a graph with nodes and edges

1 visualizzazione (ultimi 30 giorni)
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))');

Risposta accettata

Steven Lord
Steven Lord il 29 Ago 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', '--')

Più risposte (0)

Categorie

Scopri di più su Graph and Network Algorithms in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by