How to add two tables

60 ビュー (過去 30 日間)
JFz
JFz 2015 年 9 月 23 日
コメント済み: JFz 2015 年 9 月 24 日
I have two tables of different number of rows but with same structure. They both have 3 columns, the 3rd column is numbers. How to create a new table which has the common rows on column 1 and column2 and sum on column 3?
Thanks.
Jennifer

採用された回答

Kirby Fears
Kirby Fears 2015 年 9 月 23 日
編集済み: Kirby Fears 2015 年 9 月 23 日
Hi Jennifer,
Here's code with sample data that performs your task.
data=magic(3); % sample data
% creating sample tables 1 and 2
table1=array2table(data,'VariableNames',{'key1','key2','values'});
table2=array2table(data,'VariableNames',{'key1','key2','values'});
% joining tables on key1 and key2 only
tablejoin=join(table1,table2,'Keys',{'key1','key2'});
% add value columns together and delete the extra column
tablejoin.values_table1=tablejoin.values_table1+tablejoin.values_table2;
tablejoin.values_table2=[];
Hope this helps.
  5 件のコメント
JFz
JFz 2015 年 9 月 24 日
I tried to use innerjoin but I got 0 row in the resulting table.
JFz
JFz 2015 年 9 月 24 日
Hi,
Thanks Kirby,
I got it! I used innerjoin on two table and it worked!

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by