简体   繁体   中英

Concatenate cells

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,

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM