如何组合两个单元阵列的数据?(复制)

时间:2021-06-18 12:15:19

This question already has an answer here:

这个问题已经有了答案:

I have two cell arrays cellA and cellB each having 1060 elements.The size of each cell within cell array cellA is 870*1 and the same for cellB is 16*1. I want to combine these two into a third cell array cellC. Not combine the cells themselves but the data of each, such that after combining their size becomes 886*1.

我有两个单元数组cellA和cellB,每个都有1060个元素。cell array cellA中的每个单元格的大小为870*1,cellB的大小为16*1。我想把这两个组合成一个第三个单元格数组cellC。不是合并单元格本身,而是每个单元格的数据,这样在合并它们的大小之后就变成了886*1。

Ive tried these commands but they combine the cells and not the data within each

我尝试过这些命令,但它们结合了单元格,而不是每个单元内的数据。

cellC = [cellA cellB];
cellC = [{cellA} {cellB}];

3 个解决方案

#1


3  

Try this:

试试这个:

cellC=cellfun(@(x,y) [x y], cellA, cellB, 'UniformOutput', false);

#2


1  

Convert each cell array to a matrix by concatenating cells horizontally; then concatenate those matrices vertically; and convert back to a cell array of columns:

通过水平连接单元格,将每个单元格数组转换为一个矩阵;然后垂直连接这些矩阵;并将其转换回单元列数组:

cellC = mat2cell([[cellA{:}]; [cellB{:}]], numel(cellA{1})+numel(cellB{1}), [1 1]);

Example:

例子:

>> cellA = {(1:4).', (11:14).'};
>> cellB = {(101:103).', (111:113).'};
>> cellC = mat2cell([[cellA{:}]; [cellB{:}]], numel(cellA{1})+numel(cellB{1}), [1 1])
cellC = 
    [7x1 double]    [7x1 double]
>> cellC{:}
ans =
     1
     2
     3
     4
   101
   102
   103
ans =
    11
    12
    13
    14
   111
   112
   113

#3


1  

I just do something simple like:

我只是做一些简单的事情,比如:

cellC = {cellA{:} cellB{:}};

This will result in a cell array that is 886 long. From your question, I'm not sure if you wanted a regular array (which is what the other answers above will give) or a cell array. If you're Ok with getting a cell array, this is a pretty simple way of doing it.

这将导致一个长886的单元格数组。从您的问题中,我不确定您是想要一个常规数组(上面的其他答案将给出)还是单元数组。如果您对获取单元格数组没有问题,这是一种非常简单的方法。

#1


3  

Try this:

试试这个:

cellC=cellfun(@(x,y) [x y], cellA, cellB, 'UniformOutput', false);

#2


1  

Convert each cell array to a matrix by concatenating cells horizontally; then concatenate those matrices vertically; and convert back to a cell array of columns:

通过水平连接单元格,将每个单元格数组转换为一个矩阵;然后垂直连接这些矩阵;并将其转换回单元列数组:

cellC = mat2cell([[cellA{:}]; [cellB{:}]], numel(cellA{1})+numel(cellB{1}), [1 1]);

Example:

例子:

>> cellA = {(1:4).', (11:14).'};
>> cellB = {(101:103).', (111:113).'};
>> cellC = mat2cell([[cellA{:}]; [cellB{:}]], numel(cellA{1})+numel(cellB{1}), [1 1])
cellC = 
    [7x1 double]    [7x1 double]
>> cellC{:}
ans =
     1
     2
     3
     4
   101
   102
   103
ans =
    11
    12
    13
    14
   111
   112
   113

#3


1  

I just do something simple like:

我只是做一些简单的事情,比如:

cellC = {cellA{:} cellB{:}};

This will result in a cell array that is 886 long. From your question, I'm not sure if you wanted a regular array (which is what the other answers above will give) or a cell array. If you're Ok with getting a cell array, this is a pretty simple way of doing it.

这将导致一个长886的单元格数组。从您的问题中,我不确定您是想要一个常规数组(上面的其他答案将给出)还是单元数组。如果您对获取单元格数组没有问题,这是一种非常简单的方法。