Reputation: 89
I have a cell array containing cells like the following:
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>
Each cell contains numerical values like A{1,1}=[1.6386e+03] [1589] [406.9268] [184.6770]
Given that a={'el1','el2','el3','el4'}
, i would like to obtain an output B of the form:
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}...]
...
How can this be performed without writing every cell(i.e. A{1,1};A{1,2}...)
Upvotes: 0
Views: 77
Reputation: 12345
You can use concatenation (cat
) and indexing to get this result:
%Create some inputs
A = arrayfun(@(~)num2cell(randn(1,4)),zeros(10,3), 'uniformoutput',false);
a={'el1','el2','el3','el4'};
Then to create your B{1} cell array:
%Vertically concatenate the a header with all elements in the first column of A
B{1} = cat(1, a, A{:,1})
To create all of 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
Upvotes: 0