将 4 table 组为一组,每个 table 为一列?

Group 4 tables into one, with each table as one column?

我在 Matlab 中有 4 个 tables (A,B,C,D),有 102 列(例如 XYWZ),均包含 52000 行(例如 0,1,2,...)。

我想将它们合并成一个包含所有数据的大 table。

这是我想要的输出:


T   Column_names   A    B    C     D  

0       X          a(0)  b(0)  c(0)  d(0)
0       Y          a(0)  b(0)  c(0)  d(0)
0       W          a(0)  b(0)  c(0)  d(0)
0       Z          a(0)  b(0)  c(0)  d(0)
1       X          a(1)  b(1)  c(1)  d(1)
1       y          a(1)  b(1)  c(1)  d(1)
1       w          a(1)  b(1)  c(1)  d(1)
1       z          a(1)  b(1)  c(1)  d(1)
2    ...
...

我创建了一个 3 tables (A,B,C) 的示例,每个都有 3 列 (X,Y,Z) 和 4 行。

那么下面的步骤就可以达到你想要的...

  1. 添加行索引 T.

  2. 很简单
  3. 然后您可以使用 stack 创建一个高 table 的柱子堆叠(并标记为新柱子)

  4. 最后,outerjoin 会将所有 table 合并在一起。您可以将它们连接起来,但这有两个缺点

    • 您将不得不处理重复的列名
    • 您必须假设这些行的顺序相同。

代码如下,详见评论。

% Dummy data
X = (1:12).';
Y = rand(12,1);
Z = primes(40).';
% Create tables with 4 rows each
A = table( X(1:4), Y(1:4), Z(1:4), 'VariableNames', {'X','Y','Z'} );
B = table( X(5:8), Y(5:8), Z(5:8), 'VariableNames', {'X','Y','Z'} );
C = table( X(9:12), Y(9:12), Z(9:12), 'VariableNames', {'X','Y','Z'} );

% Add the row index T
A.T = (1:size(A,1)).';
B.T = (1:size(B,1)).';
C.T = (1:size(C,1)).';

% Joining
% First, stack the tables to get column names as a column
As = stack( A, {'X','Y','Z'}, 'IndexVariableName', 'Column_names', 'NewDataVariableName', 'A' );
Bs = stack( B, {'X','Y','Z'}, 'IndexVariableName', 'Column_names', 'NewDataVariableName', 'B' );
Cs = stack( C, {'X','Y','Z'}, 'IndexVariableName', 'Column_names', 'NewDataVariableName', 'C' );

% Now just concatenate the tables.
% We can do this robustly with a 'join'.
tbls = {As,Bs,Cs};
% Loop over the tables for greatest flexibility
output = tbls{1};
for ii = 2:numel(tbls)
    output = outerjoin( output, tbls{ii}, 'Keys', {'T','Column_names'}, 'MergeKeys', true );
end

输出: