Modify the weight of few edges in Graph

15 views (last 30 days)
I have the following graph with 4 edges.
tail = [1 2 3 4];
head = [2 3 4 5];
G = graph(tail,head)
G.Edges.Value = zeros(4,1)
[P,d,edgepath] = shortestpath(G,1,3)
G.Edges(edgepath,:).Value = [2 2]
The edge weights are already assigned and I want to modify it later in my code.To assign new weights to the edges between the nodes 1 and 3, I used the command shortestpath to extract the edge numbers;the last line of the code assigns new weights.
I get the following error. Any suggestions on alternate ways of modifying the edge values/weights?
Error using graph/subsasgn (line 45)
Direct editing of edges is not supported. Use addedge or rmedge instead.

Accepted Answer

Steven Lord
Steven Lord on 14 Dec 2018
Don't try to index individual rows of the Edges table to modify properties of the edges. [You could view individual rows of the Edges table using indexing as shown in the "Add Edge Weights" example on this documentation page but you can't change them.] Access the variable inside the table then index individual rows of that variable.
G.Edges.Value(edgepath, :) = [2, 2]
G.Edges % Show the updated table
If you were doing this just so you could increase the width of the lines used to draw those edges, you could use the highlight function on the plotted graph instead.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!