如何从两个单独的表中生成单元格数组?
我有一个包含数字的Excel数据集,看起来像这样:
A1 A3 A5 A7 A9
A2 A4 A6 A8 A0
和
B1 B3 B5 B7 B9
B2 B4 B6 B8 B0
其中A7或B0只是数字的替身。
我可以将它们作为表导入为单独的“表”变量,但最后 我想创建一个像这样的单元格数组:
[A1,B1] [A3,B3] [A5,B5] [A7,B7] [A9,B9]
[A2,B2] [A4,B4] [A6,B6] [A8,B8] [A0,B0]
我该怎么做? 谢谢!
答案 0 :(得分:1)
您可以将表转换为数组,然后使用arrayfun
将它们压缩在一起:
% Here's your example tables:
a = reshape(1:10, [2 5]);
b = reshape(11:20, [2 5]);
ta = array2table(a);
tb = array2table(b);
% Convert them to arrays:
a1 = table2array(ta);
b1 = table2array(tb);
% Zip them together with arrayfun:
c = arrayfun(@(x,y) {[x,y]}, a1, b1)
或更简洁地说,跳过table2array步骤:
c = arrayfun(@(x,y) {[x,y]}, ta.Variables, tb.Variables);
结果:
>> c = arrayfun(@(x,y) {[x,y]}, a1, b1)
c =
2×5 cell array
{1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double}
{1×2 double} {1×2 double} {1×2 double} {1×2 double} {1×2 double}