Clear Filters
Clear Filters

how can i combine contents of two cell arrays into one cell array of same length

1 view (last 30 days)
I have two cell arrays
Bill = {[1 2 3 4 5];[2 4 6 8 10]};
Ted = {[6 7 8 9 10];[12 14 16 18 20]};
and I want to merge them together so that
Excell = {[1 2 3 4 5 6 7 8 9 10]; [2 4 6 8 10 12 14 16 18 20]};
the only thing I can think of to combine them is
Excell = cellfun(@(v,w) [v,w],Bill,Ted,'UniformOutput',0)
but I imagine that there is a better more compact way to do this
thanks

Accepted Answer

Geoff Hayes
Geoff Hayes on 14 Nov 2014
Joshua - I think that your solution works well. An alternative is the following
Excell = mat2cell(cell2mat([Bill , Ted]),[1 1]);
where we just combine the two cell arrays as [Bill , Ted] and then convert it to a 2x10 matrix with cell2mat. We then convert back to a cell array as mat2cell(...,[1 1]) where we use the [1 1] to indicate the row dimensions of the new cell array.

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!