MATLAB Answers

0

Modify the weight of few edges in Graph

Asked by Deepa Maheshvare on 14 Dec 2018
Latest activity Answered by Steven Lord
on 14 Dec 2018
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.

  0 Comments

Sign in to comment.

Tags

Products

1 Answer

Answer by Steven Lord
on 14 Dec 2018
 Accepted Answer

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.

  0 Comments

Sign in to comment.