Merge relative cells elements into single cell

I have:
A = {[1:2]';[1:3]';[1:4]'};
B = {[2:3]';[2:4]';[2:5]'};
C = {[3:4]';[3:5]';[3:6]'};
I would like to merge the above listed cells to get the following:
D = {[1:2;2:3;3:4]';[1:3;2:4;3:5]';[1:4;2:5;3:6]'};
I tried something like this but can't get my head around it:
D = cellfun(@(a,b,c) {a{:} b{:} c{:}},A,B,C,'UniformOutput',false);
Can someone please help?
Thank you!

 Accepted Answer

D = cellfun(@vertcat,A,B,C,'UniformOutput',false);

3 Comments

It works very well, thank you!
But what if:
A = {{'part1';'part2'};{'part1';'part2';'part3'};{'part1';'part2';'part3';'part4'}};
B = {[1 2 3;2 3 4];[1 2 3;2 3 4;3 4 5];[1 2 3;2 3 4;3 4 5;4 5 6]};
while C remains:
C = {[3:4]';[3:5]';[3:6]'};
How can I get:
D = {{'part1',[1 2 3],3;'part2',[2 3 4],4};{'part1',[1 2 3],3;'part2',[2 3 4],4;'part3',[3 4 5],5};{'part1',[1 2 3],3;'part2',[2 3 4],4;'part3',[3 4 5],5;'part4',[4 5 6],6}};
@sgorratoad: that is a completely different set of operations. In your question you simply concatenated the cell contents. Now you have changed the cell arrays to contain nested cells, you want to split up the numeric vectors, and the contents are not concatenated at all but are put into nested cells. So, quite different to your question, but still possible using cellfun:
>> A = {{'part1';'part2'};{'part1';'part2';'part3'};{'part1';'part2';'part3';'part4'}};
>> B = {[1 2 3;2 3 4];[1 2 3;2 3 4;3 4 5];[1 2 3;2 3 4;3 4 5;4 5 6]};
>> C = {[3:4]';[3:5]';[3:6]'};
>> D = cellfun(@(a,b,c)[a,num2cell(b,2),num2cell(c)],A,B,C,'uni',0);
@Stephen Cobeldick: Thank you very much!!! Sorry if my real problem turned out to need a completely different solution, it's just that I don't usually like to have straight away answers to use without trying to get there myself... that is why I asked for the solution of what seemed to me to be the basic problem, to then get the solution to my problem myself by elaborating the basic solution. Unfortunately this wasn't the case...!

Sign in to comment.

More Answers (0)

Products

Release

R2017b

Asked:

on 28 Jun 2018

Commented:

on 4 Jul 2018

Community Treasure Hunt

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

Start Hunting!