sortrows graph edges .
조회 수: 4 (최근 30일)
이전 댓글 표시
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.
답변 (2개)
Christine Tobler
2020년 6월 15일
편집: Christine Tobler
2020년 6월 15일
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, :);
댓글 수: 7
KIN WONG
2021년 1월 7일
@Steven Lord
I encountered the same issue. Based on a little bit of testing, it appears to me that the graph constructor uses command "sortrows" to preprocess the Edges table.
Since this is a crucial aspect needed to maintain correspondence between edge index and additional edge-associated data, could this fact be (1) made official and permanent, and (2) documented on the MATLAB help page?
Namely:
- On the help page https://www.mathworks.com/help/releases/R2020b/matlab/ref/graph.html#mw_9ba5bc65-4397-4518-9d74-9312a4becabd
- Under these three places:
- (i) Under "usage description", under constructor "graph(s, t)"
- (ii) Under "properties", under "Edges"
- (iii) Under "Compatibility Considerations"
- Mention that: "graph" always apply "sortrows" to the EdgeTable upon graph construction.
Please consider accepting this documentation change request. Thank you.
Christine Tobler
2021년 1월 8일
I've passed this information along, thank you for the detailed comments. A note on "Compatibility Considerations" - we only use these when a behavior has changed, which isn't the case here since the Edges table has always been sorted and will stay so in the future.
Walter Roberson
2020년 6월 15일
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,:);
참고 항목
카테고리
Help Center 및 File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!