Question on using addedge()

2 views (last 30 days)
Deepa Maheshvare
Deepa Maheshvare on 14 Dec 2018
Commented: Steven Lord on 14 Dec 2018
How do we use addedge to assign weights to a graph containing edges with multiple edge weigths(Value1 and Value2, in my example)?
tail = [1 2 3 4];
head = [2 3 4 5];
G = graph(tail,head)
G.Edges.Value1 = ones(4,1)
G.Edges.Value2 = zeros(4,1)
G = addedge(G,5,6,1) %I want to assign 1 to Value1 to the new edge created between nodes 5,6
I obtain the following error.
Error using graph/addedge (line 139)
Unable to add weighted edges to an unweighted graph.
Any suggestions?
  3 Comments
Deepa Maheshvare
Deepa Maheshvare on 14 Dec 2018
Edited: Deepa Maheshvare on 14 Dec 2018
I meant, assigning a value of 1 to the newly added edge in the edge property named Value1 (here->G.Edges.Value1 = ones(4,1))
Deepa Maheshvare
Deepa Maheshvare on 14 Dec 2018
Edited: Deepa Maheshvare on 14 Dec 2018
Let's say the graph is weighted, then H = addedge(G,s,t,w) works for Graph.Edges.Weight.
I'm trying to do the same for custom attributes (Graph.Edges.Value1) and I couldn't succeed.
Hope I was able to explain the problem

Sign in to comment.

Accepted Answer

Jan
Jan on 14 Dec 2018
Edited: Jan on 14 Dec 2018
Do you mean:
G = addedge(G,5,6)
G.Edges.Value1 = ones(5,1)
Or
T = table(1, 0, 'VariableNames', {'Value1', 'Value2'});
G = addedge(G, 5, 6, T)
  1 Comment
Steven Lord
Steven Lord on 14 Dec 2018
Or you can build a table with variables EndNodes and your custom attribute names and call addedge with the graph and the table as the two inputs. The "Add Edges with Attributes to Graph" example on the addedge documentation page shows how to do this.

Sign in to comment.

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!