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

darova
darova 2020년 6월 15일
what does it mean?
Rub Ron
Rub Ron 2020년 6월 15일
@darova index is the new position sof the edges. Considering the inital position (as listed) of the edges were: [1 2 3 4 ... 300]

댓글을 달려면 로그인하십시오.

답변 (2개)

Christine Tobler
Christine Tobler 2020년 6월 15일
편집: Christine Tobler 2020년 6월 15일

2 개 추천

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

Rub Ron
Rub Ron 2020년 6월 15일
"it is always sorted by the nodes in the graph" But it is possible to sort the nodes of a graph (thus edges are re-accommodated) using reordernodes. A pity similar feature does not exist for edges. The use of another variable to stored the order is what I am doing currently, but other variables related to the graph (ie a table with more info about the edges) are not consistent with the data displayed in the graph and this might lead to confusion.
Steven Lord
Steven Lord 2020년 6월 15일
If you have a table array with additional information about the edges consider passing that edges table into the graph function to create the graph in the first place. See the "Graph Construction with Tables" example on this documentation page and the "Add Edges with Attributes to Graph" on this documentation page for more information.
That way the information about the edges is stored alongside the edges themselves, rather than in a separate variable that could become out of sync with the graph.
Rub Ron
Rub Ron 2020년 7월 19일
편집: Rub Ron 2020년 7월 19일
@Steven Lord, thanks but your suggestion does not help either. In fact by creating a graph based on a table (even if adding new egdes as tables) the edges of the graph are reorder automatically to an undesired order. It can be seen in the example you referred "Add Edges with Attributes to Graph"
Steven Lord
Steven Lord 2020년 7월 19일
And if you have additional information associated with each edge, that additional information will be reordered along with the edge EndNodes.
Don't think of it as "edge 5 has AdditionalData = 42." Think of it as "the edge between nodes 6 and 7 has AdditionalData = 42."
Rub Ron
Rub Ron 2020년 7월 19일
@Steven Lord Thank you again for your suggestion. Initially both the graph and the table had the same order. The table's order was changed, that is why the graph order is desired to have the same oder as the table. Having all the information stored in the graph does not seem suitable in my case.
KIN WONG
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:
Please consider accepting this documentation change request. Thank you.
Christine Tobler
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
Walter Roberson 2020년 6월 15일

0 개 추천

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,:);

댓글 수: 1

Rub Ron
Rub Ron 2020년 7월 19일
Hi, unfortunately in my case G is not a table, it is a graph. So, this does not work for me.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2020년 6월 6일

댓글:

2021년 1월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by