sortrows graph edges .
9 views (last 30 days)
I have a graphs G. The G.Edges has 300 elements. I would like to sort the edges of my graph based on a given index (of size 300x1). I tried:
G.Edges = sortrows(G.Edges,index);
but it gives me an error:
Error using tabular/sortrows (line 57)
Variable index exceeds table dimensions.
I also tried to sort it based in the weight of the edges, but it does not work either.
G.Edges = sortrows(G.Edges,'Weight','ascend');
Any help will be appreciatted.
Christine Tobler on 15 Jun 2020
Edited: Christine Tobler on 15 Jun 2020
The variable G.Edges.EndNodes of a graph can't be modified, it is always sorted by the nodes in the graph. This is so that the presentation is standardized, meaning that two graphs that have the same nodes and edges will present as the same.
You can save the sorted edges table into another variable, though:
weightSortedEdges = sortrows(g.Edges, 'Weight')
if this index is captured as the weight of each edge, or based on the index variable:
[~, ind] = sort(index);
indexSortedEdges = g.Edges(ind, :);
Walter Roberson on 15 Jun 2020
The second parameter to sortrows() being applied to a table, must be either 'rownames' or indications of the variables to sort on. A numeric vector can be given, in which case the entries represent variable numbers -- which would be 1 to the width of the Edges table, not to the height of the Edges table.
If you want the edges table to be in a particular order, then
G.Edges = G.Edges(index,:);