フィルターのクリア

How do i combine many tables by respective column content?

1 回表示 (過去 30 日間)
ahmed obaid
ahmed obaid 2017 年 4 月 3 日
編集済み: Andrei Bobrov 2017 年 4 月 3 日
Dear Experiences ...
i have three tables ( table A, table B and table C)
table A look like following where first columns contain unique names, and second column contain public names (may contain repeated names)..
name sun_name
____________
Amy 'tomy'
Bob 'tomy'
Hol 'salmon'
Har 'cookies'
Sal 'pizza'
second column B look like following
sun_name var1(B).....varn(B)
____________ _______________
'tomy' 0.0 .... 4.5
'tomy' 4.5 .... 5.2
'salmon' etc..
'cookies'
'pizza'
table C look like following
sun_name var1(c).....varm(c)
____________ _______________
'tomy' 0.0 .... 4.5
'tomy' 4.5 .... 5.2
'salmon' etc..
'cookies'
'pizza'
result table (out) perhaps include all columns .. as follow: out
name sun_name var1(B)...varn(B) var1(C)...varm(C)
the problem here content of name field in table A must be repeated based on other tables ? how to do that please.

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 4 月 3 日
編集済み: Andrei Bobrov 2017 年 4 月 3 日
Maybe so?
Table_out = [A,B(:,2:end),C(:,2:end)];
Added:
As said by Guillaume about innerjoin:
A = readtable('1.xls');
B = readtable('2.xls');
C = readtable('3.xls');
Tout = sortrows(innerjoin(A,innerjoin(B,C)),1);
  2 件のコメント
Image Analyst
Image Analyst 2017 年 4 月 3 日
Try various join methods, like join and outerjoin.
ahmed obaid
ahmed obaid 2017 年 4 月 3 日
ok, i have treated join tables and reach to my goal .. thanks a lot

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

その他の回答 (1 件)

Guillaume
Guillaume 2017 年 4 月 3 日
I don't see how your excel tables relate to your original question. There's no repeated key in any of them.
Going back to your original question, it looks like you want innerjoin. I'm not too clear on the relationship between B and C. Are you supposed to have 4 'tomy' after joining these two? Or do B and C have the same number of row and must simply be concatenated:
  • option 1: innerjoin
D = innerjoin(B, C);
  • option 2: plain concatenation (probably?)
D = [B, C(:, 2:end)];
The join with A is an innerjoin
A = table({'Amy'; 'Bob'; 'Hol'; 'Har'; 'Sal'}, {'tomy'; 'tomy'; 'salmon'; 'cookies'; 'pizza'}, 'VariableNames', {'name', 'sun_name'})
B = table({'tomy'; 'tomy'; 'salmon'; 'cookies'; 'pizza'}, [0; 4.5; 2; 3; 4], 'VariableNames', {'sun_name', 'var1b'})
C = table({'tomy', 'tomy', 'salmon', 'cookies', 'pizza'}', (10:10:50)', 'VariableNames', {'sun_name', 'var1c'})
D = innerjoin(A, [B, C(:, 2:end)])
results in:
7×4 table
name sun_name var1b var1c
_____ _________ _____ _____
'Har' 'cookies' 3 40
'Sal' 'pizza' 4 50
'Hol' 'salmon' 2 30
'Amy' 'tomy' 0 10
'Amy' 'tomy' 4.5 20
'Bob' 'tomy' 0 10
'Bob' 'tomy' 4.5 20

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by