Displaying weights of edges in command window

Hi, I am working on the shortest path problem and I want to be able to display the total weights between 2 points in the command window. I've been able to display the route it has taken but I want it to display the weights of the edges as well
Thanks

 Respuesta aceptada

Steven Lord
Steven Lord el 3 de Nov. de 2017
If you're using the shortestpath method of a graph or digraph object, call it with two outputs.
If you're using a graph or digraph and want to find the Weight for a particular edge, look at the Edges table, possibly in conjunction with findedge. Using the digraph created on this page with the Weights:
s = [1 1 2 2 3];
t = [2 4 3 4 4];
G = digraph(s,t);
G.Edges.Weight = [10 20 30 40 50]';
G
G.Edges
I can extract the weights of the edge between 1 and 4 with either of these lines:
G.Edges.Weight(findedge(G, 1, 4))
G.Edges{findedge(G, 1, 4), 'Weight'}
Of course, the shortestpath method gives you the edge IDs, so you don't need to use findedge here. Since edge (1, 4) is the second edge:
G.Edges{2, 'Weight'}

5 comentarios

Waseem Hussain
Waseem Hussain el 3 de Nov. de 2017
Editada: Waseem Hussain el 3 de Nov. de 2017
This is my code for this
G = digraph(DG,Names);
P = plot(G,'XData',x,'YData',y,'NodeColor','m');
set(gca,'XTick',[], 'YTick', [])
V = shortestpath(G,'Node1','Point2');
disp(V); %displays route taken in command window%
highlight(P,V,'EdgeColor','r','NodeColor','g');
How would I get yours to work?
What I've done is this
Distance = G.Edges.Weight(findedge(G,'Point1','Point2'));
But it returns with this Subscript indices must be either real positive integers or logical
Steven Lord
Steven Lord el 3 de Nov. de 2017
You just want the total weight of the path stored in V? Call shortestpath with two outputs.
You want the weight of each of the edges whose IDs are stored in V? Use G.Edges.Weight(V). There's no need to use findedge here, because V contains the edge IDs.
The reason you received that error is likely that there is no edge from 'Point1' to 'Point2' in G.
Waseem Hussain
Waseem Hussain el 3 de Nov. de 2017
Yes I just want the total weight of the path stored in V. How do I call shortest path with 2 outputs?
Steven Lord
Steven Lord el 3 de Nov. de 2017
The second and third examples on the documentation page for shortestpath to which I linked in my answer show how to do that. The third example also includes a call to highlight to show the path, as you did in your comment from about two hours ago.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Graph and Network Algorithms en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Nov. de 2017

Comentada:

el 3 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by