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 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