Combine many cell arrays into one cell array (Matlab)

强颜欢笑 提交于 2019-12-10 22:27:12

问题


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'}

回答1:


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'}];



回答2:


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

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


来源:https://stackoverflow.com/questions/43446790/combine-many-cell-arrays-into-one-cell-array-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!