How do you vertically concatenate the rows of a cell array consisting of vectors of numbers?

11 views (last 30 days)
I want to vertically concatenate the elements of a cell array so that, for example, an array that is 3 x 9 becomes 1 x 9. How do I do that? The following concatenates everything into one dimension, which isn't what I want:
vertcat(cellarray{:});
  2 Comments
L'O.G.
L'O.G. on 30 Mar 2022
Edited: L'O.G. on 30 Mar 2022
Each element of the cell array contains an N x 1 vector where N is not necessarily the same for each vector and the elements of the vector are numbers with double precision.

Sign in to comment.

Accepted Answer

per isakson
per isakson on 30 Mar 2022
Example
m = magic(3);
m = [m,m,m];
cac = num2cell(m); % sample data
arrayfun( @(ix) vertcat(cac{:,ix}), [1:9], 'uni',false )
ans = 1×9 cell array
{3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double}
Is this what you look for?

More Answers (2)

Simon Chan
Simon Chan on 30 Mar 2022
Tru the following:
cellfun(@(x) cell2mat(x),num2cell(a,1),'uni',0)

Walter Roberson
Walter Roberson on 30 Mar 2022
Output = arrayfun(@(ColIdx) vertcat(YourCell{:,ColIdx}), 1:size(YourCell,2), 'uniform', 0);

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!