Sort the rows of a table based on row entries in two columns of another table
Show older comments
I am trying to sort the rows in one table (Tright) based on the order in which the rows are present in another table (Tleft)
Tright ->
EndNodes_1 EndNodes_2 var1
__________ __________ _____________
{'1' } {'5' } 4
{'10'} {'3' } 1
{'2' } {'11'} 6
{'2' } {'8' } 2
{'3' } {'14'} 9
Tleft ->
EndNodes_1 EndNodes_2
__________ __________
{'1' } {'5' }
{'2' } {'8'}
{'2' } {'11' }
{'3' } {'14'}
{'10'} {'3' }
Expected output:
T =
EndNodes_1 EndNodes_2 var1
__________ __________ _____________
{'1' } {'5' } 4
{'2' } {'8' } 2
{'2' } {'11'} 6
{'3' } {'14'} 9
{'10'} {'3' } 1
I tried the following,
T = innerjoin(Tleft,Tright)
[~, index] = sort_nat(T.EndNodes_1);
T = T(index,:)
Obtained result:
T ->
EndNodes_1 EndNodes_2 var1
__________ __________ _____________
{'1' } {'5' } 4
{'2' } {'11'} 6
{'2' } {'8' } 2
{'3' } {'14'} 9
{'10'} {'3' } 1
Any suggestions on how to obtain the expected output will be really helpful.
Code:
ar = {'1' '10' '2' '2' '3'};
br = {'5' '3' '11' '8' '14'};
cr = [4 1 6 2 9];
Tright = table(ar',br',cr','VariableNames',{'EndNodes_1','EndNodes_2','var1'})
al = {'1' '2' '2' '3' '10'};
bl = {'5' '8' '11' '14' '3'};
Tleft = table(al',bl','VariableNames',{'EndNodes_1','EndNodes_2'})
T = innerjoin(Tleft,Tright);
[~, index] = sort_nat(T.EndNodes_1);
T = T(index,:)
Accepted Answer
More Answers (0)
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!