14

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

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
Dang Khoa
  • 5,693
  • 8
  • 51
  • 80

1 Answers1

16

How about this:

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

returns:

D = 

'A'
[1]
'B'
[2]
'C'
[3]
'D'
[4]
Robert Seifert
  • 25,078
  • 11
  • 68
  • 113