如何在保持旧细胞完整的同时收集细胞?
How to pool cells while keeping the old cells intact?
我想创建一个基于 n x m 元胞数组的 n x 1 元胞数组。我想对 n x m 元胞数组的每一行进行操作,以便将所有元胞放入新数组中的单个元胞中。
例如旧的是这样的
{'a'}, {'bc'}, {'def'}, {'g'}
{'h'}, {'i'}, {'jk'}, {'lmn'}
新的是这样的
{1x4 cell}
{1x4 cell}
在第一个 {1x4 cell} 内,有 4 个单元格
{'a'}, {'bc'}, {'def'}, {'g'}
等等。怎么做?
我不想合并单元格,使其变成 {'abcdefg'}
。
如果您的输入是字符串的二维元胞数组,
c = {'a', 'bc', 'def', 'g';
'h', 'i', 'jk', 'lmn'};
所需的输出由 mat2cell
给出(尽管该函数的名称,它的第一个输入可以是任何数组,不一定是矩阵):
result = mat2cell(c, ones(1,size(c,1)), size(c,2));
这个解决方案不一定是最短的,但旨在非常容易理解。
% define your NxM cell array
% it is 2x4
x = [{'a'}, {'bc'}, {'def'}, {'g'} ; ...
{'h'}, {'i'}, {'jk'}, {'lmn'} ]
% number of rows for the cell array
numRows = size(x,1);
% preallocate the output
y = cell(numRows, 1);
% iterate over each row
for k = 1 : numRows
% get one row of the cell array
y{k} = x(1,:)
end
我想创建一个基于 n x m 元胞数组的 n x 1 元胞数组。我想对 n x m 元胞数组的每一行进行操作,以便将所有元胞放入新数组中的单个元胞中。 例如旧的是这样的
{'a'}, {'bc'}, {'def'}, {'g'}
{'h'}, {'i'}, {'jk'}, {'lmn'}
新的是这样的
{1x4 cell}
{1x4 cell}
在第一个 {1x4 cell} 内,有 4 个单元格
{'a'}, {'bc'}, {'def'}, {'g'}
等等。怎么做?
我不想合并单元格,使其变成 {'abcdefg'}
。
如果您的输入是字符串的二维元胞数组,
c = {'a', 'bc', 'def', 'g';
'h', 'i', 'jk', 'lmn'};
所需的输出由 mat2cell
给出(尽管该函数的名称,它的第一个输入可以是任何数组,不一定是矩阵):
result = mat2cell(c, ones(1,size(c,1)), size(c,2));
这个解决方案不一定是最短的,但旨在非常容易理解。
% define your NxM cell array
% it is 2x4
x = [{'a'}, {'bc'}, {'def'}, {'g'} ; ...
{'h'}, {'i'}, {'jk'}, {'lmn'} ]
% number of rows for the cell array
numRows = size(x,1);
% preallocate the output
y = cell(numRows, 1);
% iterate over each row
for k = 1 : numRows
% get one row of the cell array
y{k} = x(1,:)
end