Adding weights to a graph

7 次查看(过去 30 天)
Deepa Maheshvare
Deepa Maheshvare 2019-9-8
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?

回答(1 个)

Walter Roberson
Walter Roberson 2019-9-8
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.

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by