kgk
kgk

Reputation: 659

Combine many cell arrays into one cell array (Matlab)

I have 3 cell arrays :

c1={'a','b','c'}
c2={'a2','b2','c2'}
c3={'a3','b3','c3'}

How can I combine those 3 cell arrays into 1 cell array C as follows:

C={'a','b','c','a2','b2','c2','a3','b3','c3'}

Upvotes: 2

Views: 433

Answers (2)

Wolfie
Wolfie

Reputation: 30047

You can simply use square brackets;

c = [c1, c2, c3]

% c = {'a'    'b'    'c'    'a2'    'b2'    'c2'    'a3'    'b3'    'c3'}

This can be used when appending items to the end of a cell too,

d1 = {'a', 'b', 'c', 'd'};
d2 = [d1, {'e'}];

Upvotes: 2

rahnema1
rahnema1

Reputation: 15837

With colon you can create comma separated lists and then concatenate them:

c = {c1{:}, c2{:} ,c3{:}}

Upvotes: 1

Related Questions