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일

1 개 추천

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일
Thanks so much! It works! Works exactly as I wished. The only thing is that, what is the two tables are not of the same number of rows? Say, one table has 4 rows, the other only 3 rows.
JFz
JFz 2015년 9월 24일
Hi, Kirby,
I got an error:
Error using table/join (line 132) The key variables for B must contain all unique combinations of values in the key variables for A.
JFz
JFz 2015년 9월 24일
I tried to use 'Type' in the join, but I keep getting this error: Error using table/join (line 85) Invalid parameter name: Type.
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개)

카테고리

도움말 센터File Exchange에서 Time Series Events에 대해 자세히 알아보기

제품

태그

질문:

JFz
2015년 9월 23일

댓글:

JFz
2015년 9월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by