Concatenate cells

安稳与你 提交于 2019-12-20 03:01:04

问题


I have two cells,

A =
 [100x2 double]    [80x2 double]    [50x2 double]
B =
 [100x5 double]    [80x5 double]    [50x5 double]

How can I Concatenate them to get something like C = cat(2,A,B) for each array. C must be,

C =
 [100x7 double]    [80x7 double]    [50x7 double]

Thanks,


回答1:


C = cellfun(@(a, b) [a b], A, B, 'UniformOutput', false);

This will create the cell array C for you.

Example:

>> A = { zeros(100,2), zeros(200,2) };
>> B = { ones(100,5), ones(200,5)};
>> C = cellfun(@(a, b) [a b], A, B, 'UniformOutput', false);



回答2:


You probably need some kind of loop:

C = arrayfun(@(n) [A{:,n} B{:,n}], 1:numel(A), 'uniformoutput', 0);

Of course, if the number of cells in A (and B) is fixed, you can replace the loop by an enumeration:

C = {[A{:,1} B{:,1}] [A{:,2} B{:,2}] [A{:,3} B{:,3}]};


来源:https://stackoverflow.com/questions/26442363/concatenate-cells

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