How can we access individual edges in a Graph ?

1 visualizzazione (ultimi 30 giorni)
charu shree il 21 Apr 2023
Risposto: Steven Lord il 21 Apr 2023
Hello all, I have the following Graph:
I know that with G.Edges , we can know the edges.
But I am not getting, how can I access individual edge of this Graph.
Any help in this regard will be highly appreciated.
sr = [1,2,2,2,3,3,3,4,5];
ta = [2,3,6,8,6,4,7,6,6];
G = graph(sr,ta);
plot(G)
3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
charu shree il 21 Apr 2023
Yes sir, I want to obtain edge 8 for example
Torsten il 21 Apr 2023
G.Edges(8,:)

Accedi per commentare.

Risposta accettata

Steven Lord il 21 Apr 2023
I'd be careful asking for say "edge 8" because graph and digraph reserve the right to reorder the edges array if you insert or delete edges.
g = graph([1 2 3], [2 3 1])
g =
graph with properties: Edges: [3×1 table] Nodes: [3×0 table]
g.Edges
ans = 3×1 table
EndNodes ________ 1 2 1 3 2 3
originalEdge3 = g.Edges{3, :}
originalEdge3 = 1×2
2 3
g = addedge(g, 2, 1)
g =
graph with properties: Edges: [4×1 table] Nodes: [3×0 table]
newEdge3 = g.Edges{3, :}
newEdge3 = 1×2
1 3
originalEdge3 and newEdge3 are not the same edge. Before you ask no, there is no way to stop graph or digraph from doing this reordering.
If you want to ask if there is an edge between a source and target node, use findedge. Call it right before you need to access the edge, as adding or removing an edge can change the output of findedge as you saw above.
whichEdge = findedge(g, 2, 3)
whichEdge = 4
g.Edges(whichEdge, :)
ans = table
EndNodes ________ 2 3
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Categorie

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

Community Treasure Hunt

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

Start Hunting!

Translated by