Suppose I have a cell array A
and B
, as so:
假设我有一个单元格数组A和B,如下所示:
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:
我想通过“压缩”A和B来创建单元阵列C,如下所示:
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.)
(我从Perl那里得到了这个想法,其中List :: MoreUtils包提供了执行此操作的zip函数。该名称来自zip函数交错两个列表,如拉链。)
1 个解决方案
#1
11
How about this:
这个怎么样:
C = [A(:),B(:)].'; %'
D = C(:)
returns:
收益:
D =
'A'
[1]
'B'
[2]
'C'
[3]
'D'
[4]
#1
11
How about this:
这个怎么样:
C = [A(:),B(:)].'; %'
D = C(:)
returns:
收益:
D =
'A'
[1]
'B'
[2]
'C'
[3]
'D'
[4]