Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function to "zip" two cell arrays together? [duplicate]

Tags:

arrays

matlab

Suppose I have a cell array A and B, as so:

A = {'A' 'B' 'C' 'D'};
B = {1 2 3 4 };

I wanted to create cell array C by "zipping" A and B together, as so:

C = zip(A,B)
C = 
    'A' 1 'B' 2 'C' 3 'D' 4

Does such a function exist? (Obviously such a function would not be difficult to write, but laziness is a programmer's best friend and if such a function already exists I'd rather use that.)

(I got the idea from Perl, where the List::MoreUtils package offers the zip function that does this. The name comes from the fact that the zip function interleaves two lists, like a zipper.)

like image 649
Dang Khoa Avatar asked Nov 07 '13 17:11

Dang Khoa


1 Answers

How about this:

C = [A(:),B(:)].';   %'
D = C(:)

returns:

D = 

'A'
[1]
'B'
[2]
'C'
[3]
'D'
[4]
like image 75
Robert Seifert Avatar answered Sep 18 '22 13:09

Robert Seifert