Sort the rows of a table based on row entries in two columns of another table

3 vues (au cours des 30 derniers jours)
Deepa Maheshvare
Deepa Maheshvare le 14 Déc 2019
I've the following tables
Tleft>
EndNodes_1 EndNodes_2
__________ __________
{'1' } {'Node9' }
{'2' } {'Node10'}
{'2' } {'Node11'}
{'3' } {'Node12'}
{'3' } {'Node13'}
{'4' } {'Node14'}
{'4' } {'Node15'}
{'5' } {'Node16'}
{'6' } {'Node17'}
{'7' } {'Node18'}
{'Node9' } {'2' }
{'Node10'} {'3' }
{'Node11'} {'4' }
{'Node12'} {'5' }
{'Node13'} {'5' }
{'Node14'} {'6' }
{'Node15'} {'6' }
{'Node16'} {'7' }
{'Node17'} {'7' }
{'Node18'} {'8' }
Tright =
20×3 table
EndNodes_1 EndNodes_2 value
__________ __________ _______
{'1' } {'Node9' } {[5.2]}
{'Node9' } {'2' } {[5.2]}
{'2' } {'Node10'} {[2.6]}
{'Node10'} {'3' } {[2.6]}
{'2' } {'Node11'} {[2.6]}
{'Node11'} {'4' } {[2.6]}
{'3' } {'Node12'} {[1.3]}
{'Node12'} {'5' } {[1.3]}
{'3' } {'Node13'} {[1.3]}
{'Node13'} {'5' } {[1.3]}
{'4' } {'Node14'} {[1.3]}
{'Node14'} {'6' } {[1.3]}
{'4' } {'Node15'} {[1.3]}
{'Node15'} {'6' } {[1.3]}
{'5' } {'Node16'} {[2.6]}
{'Node16'} {'7' } {[2.6]}
{'6' } {'Node17'} {[2.6]}
{'Node17'} {'7' } {[2.6]}
{'7' } {'Node18'} {[5.2]}
{'Node18'} {'8' } {[5.2]}
Result obtained after using
T = innerjoin(Tleft,Tright)
T =
20×3 table
EndNodes_1 EndNodes_2 value
__________ __________ _______
{'1' } {'Node9' } {[5.2]}
{'2' } {'Node10'} {[2.6]}
{'2' } {'Node11'} {[2.6]}
{'3' } {'Node12'} {[1.3]}
{'3' } {'Node13'} {[1.3]}
{'4' } {'Node14'} {[1.3]}
{'4' } {'Node15'} {[1.3]}
{'5' } {'Node16'} {[2.6]}
{'6' } {'Node17'} {[2.6]}
{'7' } {'Node18'} {[5.2]}
{'Node10'} {'3' } {[2.6]}
{'Node11'} {'4' } {[2.6]}
{'Node12'} {'5' } {[1.3]}
{'Node13'} {'5' } {[1.3]}
{'Node14'} {'6' } {[1.3]}
{'Node15'} {'6' } {[1.3]}
{'Node16'} {'7' } {[2.6]}
{'Node17'} {'7' } {[2.6]}
{'Node18'} {'8' } {[5.2]}
{'Node9' } {'2' } {[5.2]}
Expected result: Rows to be in the sam eorder of rows of Tleft in T
T =
20×3 table
EndNodes_1 EndNodes_2 value
__________ __________ _______
{'1' } {'Node9' } {[5.2]}
{'2' } {'Node10'} {[2.6]}
{'2' } {'Node11'} {[2.6]}
{'3' } {'Node12'} {[1.3]}
{'3' } {'Node13'} {[1.3]}
{'4' } {'Node14'} {[1.3]}
{'4' } {'Node15'} {[1.3]}
{'5' } {'Node16'} {[2.6]}
{'6' } {'Node17'} {[2.6]}
{'7' } {'Node18'} {[5.2]}
{'Node9' } {'2' } {[5.2]}
{'Node10'} {'3' } {[2.6]}
{'Node11'} {'4' } {[2.6]}
{'Node12'} {'5' } {[1.3]}
{'Node13'} {'5' } {[1.3]}
{'Node14'} {'6' } {[1.3]}
{'Node15'} {'6' } {[1.3]}
{'Node16'} {'7' } {[2.6]}
{'Node17'} {'7' } {[2.6]}
{'Node18'} {'8' } {[5.2]}
Note: The entries are strings and cell arrays since the table is created from a graph object
  2 commentaires
dpb
dpb le 14 Déc 2019
First column is sorted lexically and since there aren't two digits in the node number string, 'Node9' sorts behind 'NodeN' for N<9. You'll have the same problem if your numeric values pass single digits anywhere in the real data set.
There's a FEX submission, whether it'll be smart enough for this case or not, I don't know.
Probably the easiest workaround if you can't fix the data format to be consistent is the list starts in order is to augment with an index vector of 1:size(Tleft,1) and then resort the final result by it.
Alternatively, fix the naming to be consistent for desired sort order.
Oh...you might look to see if there's anything like an optional parameter in innerjoin like 'stable' to preserve existing order.
Deepa Maheshvare
Deepa Maheshvare le 14 Déc 2019
Modifié(e) : Deepa Maheshvare le 14 Déc 2019
Could you please share the link to the FEX submission? Unfortunately, I couldn't find the optional parameter 'stable'.

Connectez-vous pour commenter.

Réponses (1)

Mohammad Sami
Mohammad Sami le 17 Déc 2019
inner join will return a second optional output, ileft
ileft = Index to Tleft, returned as a column vector. Each element of ileft identifies the row in Tleft that corresponds to that row in the output table or timetable, T.
You can potentially use this to resort the output in the order of tleft.
[T,ileft] = innerjoin(Tleft,Tright);
[~,iileft] = sort(ileft); % use the optional output of sort to get the correct sorting order
T = T(iileft,:);

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by