Adding weights to a graph
7 views (last 30 days)
Show older comments
Hi All,
I have the following graph.
NNode = 11;
tail = 1:NNode-1;
head = 2:NNode;
Graph = graph(tail,head);
Graph.Edges.Weight1 = randn(height(Graph.Edges),1)
Graph.Edges.Weight2 = randn(height(Graph.Edges),1)
H = Graph
H = addedge(H,1,2,Graph.Edges.Weight1(1))
H = addedge(H,3,2,Graph.Edges.Weight1(1))
plot(Graph);
When I try to add new nodes, edges and assign weights, the following error is obtained
Error using graph/addedge (line 139)
Unable to add weighted edges to an unweighted graph.
Error in Untitled (line 9)
H = addedge(H,1,2,Graph.Edges.Weight1(1))
In line 9, a weighted graph is assigned to a variable H. I am adding new edges and weights to H. I am not sure why
Unable to add weighted edges to an unweighted graph.
error appears.
Could someone look into this?
0 Comments
Answers (1)
Walter Roberson
on 8 Sep 2019
Graph = graph(tail,head);
Graph creates an undirected graph without weights, using a slightly different data structure than a graph that has weights. You need to either create the graph with weights, graph(tail,head,appropriate_weights) or else you need to assign to the Weight property of the graph, such as Graph.Weight = randn(10,1) . Once the Weight structure is there, then you can use addedge() to add edges with weights.
0 Comments
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!