Adding weights to a graph

4 ビュー (過去 30 日間)
Deepa Maheshvare
Deepa Maheshvare 2019 年 9 月 8 日
回答済み: Walter Roberson 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 Center および File ExchangeGraph and Network Algorithms についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by