How to sort all rows in one table based on one row from the table and another table
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pushti Shah
am 19 Aug. 2022
Kommentiert: Stephen23
am 22 Aug. 2022
I have two different tables one with names (table 1) and others with names and data (table 2) but the second table is out of order. I want to order the names in table 2 by the order in table 1 but also not lose the data in the row. For example:
Table 1: Table 2:
Mom Sister 25 67 89
Dad Mom 88 76 23
Sister Dad 90 45 28
and I want to sort it according to table 1 so
Sister 25 67 89
Mom 88 76 23
Dad 90 45 28
1 Kommentar
Stephen23
am 22 Aug. 2022
t1 = table({'Mom';'Dad';'Sister'},'VariableNames',{'Names'})
t2 = table({'Sister';'Mom';'Dad'},[25;88;90],[67;76;45],[89;23;28],'VariableNames',{'Names','Var1','Var2','Var3'})
t3 = join(t1,t2) % the simple MATLAB approach
Akzeptierte Antwort
Voss
am 19 Aug. 2022
Bearbeitet: Voss
am 19 Aug. 2022
t1 = table( ...
{'Mom';'Dad';'Sister'}, ...
'VariableNames',{'Names'})
t2 = table( ...
{'Sister';'Mom';'Dad'}, ...
[25;88;90], ...
[67;76;45], ...
[89;23;28], ...
'VariableNames',{'Names','Var1','Var2','Var3'})
[~,idx] = ismember(t1.Names,t2.Names);
t2 = t2(idx,:)
Siehe auch
Kategorien
Mehr zu Other Formats finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!