Combine a cell array of cell arrays to a cell array of numbers

Hi,
I have a dynamic cell array of cell arrays and I want to combine it into a cell array of data.
For example:
kdata =
Columns 1 through 4
{10x3 cell} {10x3 cell} {10x3 cell} {10x3 cell}
Columns 5 through 8
{10x3 cell} {10x3 cell} {10x3 cell} {10x3 cell}
Columns 9 through 10
{10x3 cell} {10x3 cell}
What I want:
kdata_combined =
Columns 1 through 11
'8' '7' '2' '8' '10' '5' '1' '2' '1' '4' '3'
'2' '1' '4' '2' '3' '0' '3' '3' '0' '0' '0'
'1' '1' '1' '4' '4' '4' '1' '1' '2' '4' '2'
'3' '2' '1' '1' '2' '1' '1' '1' '0' '2' '2'
'2' '5' '0' '0' '1' '2' '0' '2' '0' '1' '0'
'0' '1' '3' '1' '1' '2' '1' '0' '0' '2' '1'
'0' '4' '0' '0' '1' '0' '1' '2' '1' '1' '0'
'2' '0' '0' '0' '0' '0' '0' '0' '1' '0' '0'
'2' '0' '0' '1' '0' '1' '2' '0' '0' '3' '0'
'0' '0' '1' '0' '1' '0' '1' '0' '2' '2' '1'
Columns 12 through 22
'11' '0' '2' '11' '6' '7' '6' '0' '5' '0' '4'
'2' '7' '2' '1' '1' '4' '6' '2' '0' '5' '3'
'2' '1' '1' '1' '1' '4' '3' '1' '2' '3' '0'
'2' '1' '2' '1' '1' '4' '0' '1' '0' '1' '2'
'2' '0' '1' '1' '1' '0' '2' '1' '1' '1' '3'
'0' '0' '0' '3' '0' '4' '0' '0' '0' '0' '1'
'3' '2' '1' '2' '2' '0' '2' '0' '0' '0' '1'
'2' '0' '3' '0' '1' '2' '0' '2' '1' '1' '0'
'1' '0' '1' '1' '0' '0' '2' '0' '2' '0' '0'
'0' '0' '0' '2' '2' '0' '1' '3' '0' '0' '0'
Columns 23 through 30
'1' '1' '3' '1' '2' '4' '5' '8'
'0' '0' '5' '4' '0' '2' '2' '7'
'1' '1' '3' '9' '0' '0' '4' '0'
'3' '1' '1' '0' '3' '0' '0' '1'
'3' '1' '0' '0' '0' '1' '1' '0'
'3' '1' '0' '1' '0' '1' '0' '2'
'1' '0' '0' '1' '1' '0' '1' '0'
'7' '1' '0' '0' '0' '1' '1' '1'
'0' '0' '2' '1' '2' '0' '3' '1'
'4' '0' '1' '0' '2' '0' '2' '0'
I want to do this without any 'for' because my cell arrays are too big. And I can't concat them manually because I never know the size.
Is there anyway I can do this using some function like cell fun ?
Thanks.

1 件のコメント

per isakson
per isakson 2014 年 1 月 21 日
編集済み: per isakson 2014 年 1 月 21 日
How do you know they "are too big" to use a loop?

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2014 年 1 月 20 日
編集済み: Matt J 2014 年 1 月 20 日

0 投票

kdata_combined = cell2mat(kdata)

6 件のコメント

Eduardo
Eduardo 2014 年 1 月 20 日
Did not work for me
Error using cell2mat Cannot support cell arrays containing cell arrays or objects.
Matt J
Matt J 2014 年 1 月 20 日
Make that,
kdata_combined = cell2mat([kdata{:}])
Eduardo
Eduardo 2014 年 1 月 20 日
Thanks, Matt. But there is another error
Error using cat CAT arguments dimensions are not consistent.
Error in cell2mat (line 84) m{n} = cat(1,c{:,n});
I don't understand why. The dimensions of my cell arrays are all 10x3 or (any number)x3
Matt J
Matt J 2014 年 1 月 20 日
編集済み: Matt J 2014 年 1 月 20 日
The dimensions of my cell arrays are all 10x3 or (any number)x3
@Eduardo,
You should modify your posted example, clarifying the data you have. All of your cells there are 10x(any number) not (any number)x3. You cannot horizontally concatenate (as you have in your example) when you have varying numbers of rows.
Eduardo
Eduardo 2014 年 1 月 20 日
編集済み: Eduardo 2014 年 1 月 20 日
I usually have 1 cell array with 5, 10 or any number of cell arrays in it.
Every one of them are like 4000x3 or 4000x2. But they're all the same size. My example was wrong, sorry about that.
I attached a real .mat file with one cell array.
Matt J
Matt J 2014 年 1 月 21 日
Just do
kdata_combined=[kdata{:}];

サインインしてコメントする。

その他の回答 (2 件)

John Thompson
John Thompson 2014 年 1 月 20 日
編集済み: John Thompson 2014 年 1 月 20 日

0 投票

% Initialize fake data
cellA = cell(10,1);
for i = 1:10
cellA{i} = num2cell(round(rand(10,1)*10));
end
% RUN THIS to extract and realign nested cell arrays
cac = cell2mat(cellfun(@(x) cell2mat(x)' , cellA, 'UniformOutput', false));
per isakson
per isakson 2014 年 1 月 21 日
編集済み: per isakson 2014 年 1 月 21 日

0 投票

You have a cell array of cell arrays of strings. Your strings consist of one and in some cases two characters. The solution, which I proposed, assumed that all strings are of the same length. Since they are not you get the error
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 84)
m{n} = cat(1,c{:,n});
.
I missed the word "number" in the topic title.
Does this do the job?
cac = cat( 2, kdata{:} );
num = str2double( cac );
cac_num = num2cell( num );
or this
kdata_combined = cat( 2, kdata{:} );
both assume that kdata is .< 1 xn cell>
.
[Later]
and when kdata is .< m xn cell>
cc = repmat( kdata, 400, 1 );
tic
nrows = size( cc, 1 );
cac = cell( nrows, 1 );
for jj = 1 : nrows
cac{ jj } = cat( 2, cc{ jj, : } );
end
kdata_combined = cat( 1, cac{:} );
toc
whos('kdata_combined')
returns
Elapsed time is 0.108852 seconds.
Name Size Bytes Class Attributes
kdata_combined 114800x24 314099200 cell
.
I don't think you will find a non-loop solution that is faster

1 件のコメント

Eduardo
Eduardo 2014 年 1 月 21 日
I want the result like this:
examples = {{'1','12'},{'2','4'},{'7','10'},{'0','3'}}
example =
{1x2 cell} {1x2 cell} {1x2 cell} {1x2 cell}
result =
'1' '12' '2' '4' '7' '10' '0' '3'
This example has just one row, my real data usually have a lot of rows.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2014 年 1 月 20 日

編集済み:

2014 年 1 月 21 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by