How to find index and copy data to the another table having same label?

1 回表示 (過去 30 日間)
HCLEE
HCLEE 2022 年 11 月 9 日
コメント済み: Star Strider 2022 年 11 月 9 日
Hi,
There are 'table1' and 'table 2' which have same index labels.
The table 1 has data and I want copy them to the table 2 as shown in below picture.
I'm not sure which code is prefer to me.

採用された回答

Star Strider
Star Strider 2022 年 11 月 9 日
I would experiment by vertically concatenatating the two tables and then use sortrows on the result to sort them by the first column. I can’t fiund the appropriate documentation just now, however that would work just like vertically concatenating arrays —
Table3 = [Table1; Table2]
T1 = array2table(randi(9, 5, 4), 'VariableNames',{'1st class','2nd class','mass(kg)','conc(%)'})
T1 = 5×4 table
1st class 2nd class mass(kg) conc(%) _________ _________ ________ _______ 5 1 1 6 5 9 5 2 9 6 5 1 3 9 6 6 5 5 7 3
T2 = array2table(randi(9, 5, 4), 'VariableNames',{'1st class','2nd class','mass(kg)','conc(%)'})
T2 = 5×4 table
1st class 2nd class mass(kg) conc(%) _________ _________ ________ _______ 4 3 6 3 8 2 2 4 2 7 4 8 8 9 3 8 8 3 8 7
T3 = [T1; T2]
T3 = 10×4 table
1st class 2nd class mass(kg) conc(%) _________ _________ ________ _______ 5 1 1 6 5 9 5 2 9 6 5 1 3 9 6 6 5 5 7 3 4 3 6 3 8 2 2 4 2 7 4 8 8 9 3 8 8 3 8 7
T3 = sortrows(T3,1)
T3 = 10×4 table
1st class 2nd class mass(kg) conc(%) _________ _________ ________ _______ 2 7 4 8 3 9 6 6 4 3 6 3 5 1 1 6 5 9 5 2 5 5 7 3 8 2 2 4 8 9 3 8 8 3 8 7 9 6 5 1
That approach at least works when I test it here.
.
  2 件のコメント
HCLEE
HCLEE 2022 年 11 月 9 日
The question was not about the sortrows, but the 'data copy' to the same class which already exist in the Table 2 from the Table 1.
I'm not sure, but think that the step for finding and matching the 1st and 2nd class names is needed first. And then copying data of mass and conc. step would be followed.
Star Strider
Star Strider 2022 年 11 月 9 日
The point I make here is that you can vertically concatenate both tables, and that is likely what you want to do. The sortrows call simply sorts the first column to get the desired result (so all the ‘A’ values are at the top, the ‘B’ values next, and so for the rest).
Copying the values from one table to another would mean vertically concatenating the tables and then sorting them. This is not (at least as it appears to me) a join operation, although you can certainly experiment with that if you so desire.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by