我在 Matlab 中有 4 个表(A
、B
、C
、D
),有 102 列(例如X
、Y
、W
、Z
),全部包含 52000 行(例如0,1,2,...
)。
我想将它们合并到一个包含所有数据的大表中。
这是我想要的输出:
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 个表 ( A,B,C
)的示例,每个表有 3 列 ( X,Y,Z
) 和 4 行。
然后以下步骤实现你想要的......
添加行索引很简单T
。
然后,您可以使用stack
堆叠的列(并标记为新列)来创建一个高大的表格
最后outerjoin
将所有表合并在一起。您可以将它们连接起来,但这有两个缺点
代码如下,详情请看评论。
% 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
输出:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句