Discussion:
merge cells within a cell array
(too old to reply)
Vishi Vishi
2012-03-23 13:54:10 UTC
Permalink
Hi all,

I have a 2 x 16 cell array. Each cell has 500 x 1 cells. Instead, I want a single cell array that has 1000 x 16 (i.e. I want to vertically concatenate corresponding cells). I am play around with arrayfun but can't make it work.

Thanks,
Vishi
Matt J
2012-03-23 14:13:22 UTC
Permalink
Post by Vishi Vishi
Hi all,
I have a 2 x 16 cell array. Each cell has 500 x 1 cells. Instead, I want a single cell array that has 1000 x 16 (i.e. I want to vertically concatenate corresponding cells). I am play around with arrayfun but can't make it work.
==============

C=reshape([YourCellArray{:}],1000,16);
Vishi Vishi
2012-03-23 15:29:17 UTC
Permalink
Mine is a mixed cell array. Some of the 500x1 cells contain numbers and some contain strings. When I use reshape, I get ??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Post by Matt J
Post by Vishi Vishi
Hi all,
I have a 2 x 16 cell array. Each cell has 500 x 1 cells. Instead, I want a single cell array that has 1000 x 16 (i.e. I want to vertically concatenate corresponding cells). I am play around with arrayfun but can't make it work.
==============
C=reshape([YourCellArray{:}],1000,16);
Vishi Vishi
2012-03-23 15:33:12 UTC
Permalink
Mine is a mixed cell array. Some of the 500x1 cells contain numbers and some contain strings. When I use reshape, I get ??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Post by Matt J
Post by Vishi Vishi
Hi all,
I have a 2 x 16 cell array. Each cell has 500 x 1 cells. Instead, I want a single cell array that has 1000 x 16 (i.e. I want to vertically concatenate corresponding cells). I am play around with arrayfun but can't make it work.
==============
C=reshape([YourCellArray{:}],1000,16);
Matt J
2012-03-23 15:45:17 UTC
Permalink
Post by Vishi Vishi
Mine is a mixed cell array. Some of the 500x1 cells contain numbers and some contain strings. When I use reshape, I get ??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
============

That can only be happening if not all of your cells are 500x1.

If they are all 500x1, it doesn't matter whether the contents are string, numeric, or mixed. Below is an example that shows this, but you'll need my MAT2TILES utility to run it

http://www.mathworks.com/matlabcentral/fileexchange/35085-split-nd-array-into-equal-sized-cells
Post by Vishi Vishi
A=num2cell(rand(1000,16)); A(:,1)={'string'};
B=mat2tiles(A,[500,1]);
C=reshape([B{:}],1000,16)
Vishi Vishi
2012-03-23 18:14:18 UTC
Permalink
Thank you, Matt J! My problem was that some columns were 500x1 cells while others were 500 x 1 double. Once I converted numbers into cells via num2cell, you reshaping suggestion worked.

Thanks again,
Vishi
Post by Matt J
Post by Vishi Vishi
Mine is a mixed cell array. Some of the 500x1 cells contain numbers and some contain strings. When I use reshape, I get ??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
============
That can only be happening if not all of your cells are 500x1.
If they are all 500x1, it doesn't matter whether the contents are string, numeric, or mixed. Below is an example that shows this, but you'll need my MAT2TILES utility to run it
http://www.mathworks.com/matlabcentral/fileexchange/35085-split-nd-array-into-equal-sized-cells
Post by Vishi Vishi
A=num2cell(rand(1000,16)); A(:,1)={'string'};
B=mat2tiles(A,[500,1]);
C=reshape([B{:}],1000,16)
Loading...