concatenate tables based on table row names

How can I concatenate the following tables:
a = table([5; 76; 25; 321; 523; 21; 562; 74], 'RowNames', {'a','b','c','d','e','f','g','h'});
b = table([487; 96; 325; 98; 741; 23], 'RowNames', {'a','b','d','e','g', 'h'});
c = table([25; 325; 32; 52; 21], 'RowNames', {'a','d','e','f','g'});
...
z = table([2; 35; 2; 52; 1; 63; 45], 'RowNames', {'a','c','d','e','f','g','h'});
To obtain
Table = 8 by z
a b c ... z
a 5 487 25 ... 2
b 76 96 0 ... 0
c 25 0 0 ... 35
d 321 325 325 ... 2
e 523 98 32 ... 52
f 21 0 52 ... 1
g 562 741 21 ... 63
h 74 23 0 ... 45

2 件のコメント

Sai Gudlur
Sai Gudlur 2020 年 6 月 13 日
Hello,
Same as putting together a table with vectors or matrices.
T = [a b c] % combine them along column dimension.
T1 = [a;b;c] % combine them along row dimension.
Ronald
Ronald 2020 年 6 月 13 日
This option is not applicable to unequal tables, unfortunately

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

回答 (1 件)

Sai Gudlur
Sai Gudlur 2020 年 6 月 13 日

0 投票

You could also try "vertcat" on tables.

5 件のコメント

Ronald
Ronald 2020 年 6 月 13 日
Does not apply in this case since this a horizontal concatenation problem. However, horzcat() too can't apply in this case since there are different number of elements in the tables.
Ronald
Ronald 2020 年 6 月 13 日
Thank you Sai, I solved it using outerjoin()!
madhan ravi
madhan ravi 2020 年 6 月 13 日
編集済み: madhan ravi 2020 年 6 月 13 日
outerjoin(...) is the way to go. Ronald post it as an answer so others could benifit from it.
Sai Gudlur
Sai Gudlur 2020 年 6 月 14 日
Thank you Ronald, for outerjoin wasn't aware of this option.
Ronald
Ronald 2020 年 6 月 14 日
This is what saved my problem up there:
Using the first one (a) as the reference table
a = table({'a','b','c','d','e','f','g','h'}, [5; 76; 25; 321; 523; 21; 562; 74], 'VariableNames', {'Key','Var1'});
This solution is based on data generated within a loop. In my case, the size of newTable was being generated by a different function not included here. Nevertheless, it can be modified to suit any purpose. For instance, the tables
(a,...z)
can be stored in a struct object and the contents accessed and merged using this loop.
resTable = table(zeros(length(a),n))
for i = 1:length(n)%asuming n defines as single vector for which an enumerator can be applied
newTable = table(rowNames, tabVals, 'VariableNames', {'Key','Var2'});%Here, I create new tables in every iteration
mergedTable = outerjoin(a, newTable, 'MergeKeys', true);
resTable(:,i) = mergedTable;
end
resTable.Properties.RowNames = mergedTable.Key;%since the row names changed positions in the loop

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

カテゴリ

質問済み:

2020 年 6 月 13 日

コメント済み:

2020 年 6 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by