单元格数组内部的单元格数组元素Matlab

时间:2013-09-16 18:10:49

标签: arrays matlab

我有一个包含以下单元格的单元格数组:

A=
<1x4 cell>  <1x4 cell>  <1x4 cell>
<1x4 cell>  <1x4 cell>  <1x4 cell>
<1x4 cell>  <1x4 cell>  <1x4 cell>
<1x4 cell>  <1x4 cell>  <1x4 cell>
<1x4 cell>  <1x4 cell>  <1x4 cell>
<1x4 cell>  <1x4 cell>  <1x4 cell>
<1x4 cell>  <1x4 cell>  <1x4 cell>
<1x4 cell>  <1x4 cell>  <1x4 cell>
<1x4 cell>  <1x4 cell>  <1x4 cell>
<1x4 cell>  <1x4 cell>  <1x4 cell>

每个单元格都包含A{1,1}=[1.6386e+03] [1589] [406.9268] [184.6770]

等数值

鉴于a={'el1','el2','el3','el4'},我想获得以下形式的输出B:

B{1}=[a;A{1,1};A{2,1};A{3,1}...]
B{1}=
'el1'   'el2'   'el3'   'el4'
1638.60000000000    1589          406.926813049605                  184.676951989012
1665.10000000000    1614.60000000000    399.333905068047    362.462074500098
1709.60000000000    1657.80000000000    389.181059994089    529.870013181953
...

B{2}=[a;A{1,2};A{2,2};A{3,2}...]
...

如何在不写每个单元格的情况下执行此操作(即A {1,1}; A {1,2} ...)

1 个答案:

答案 0 :(得分:0)

您可以使用连接(cat)和索引来获得此结果:

%Create some inputs
A = arrayfun(@(~)num2cell(randn(1,4)),zeros(10,3), 'uniformoutput',false);
a={'el1','el2','el3','el4'};

然后创建B {1}单元格数组:

%Vertically concatenate the a header with all elements in the first column of A
B{1} = cat(1, a, A{:,1})

创建所有B

for ixColumn = 1:size(A,2)   %Or, loop backwards for slightly better performance.  IE "ixColumn = (size(A,2):-1:1"
    B{ixColumn } = cat(1, a, A{:,ixColumn });
end