如何在MATLAB中将行向量与自身连接起来?

时间:2017-05-30 16:16:11

标签: matlab concatenation

我有一个大小< 1x12582 cell>的行向量,我想将它与自身结合起来得到结果< 1x25164 cell>。例如:

cellint = {'gene1','gene2','gene3','gene4'};

cellout = {'gene1','gene2','gene3','gene4','gene1','gene2','gene3','gene4'};

我已尝试horzcat如下,但它没有给出正确的结果:

SS = [cellint;cellint];

1 个答案:

答案 0 :(得分:1)

Horizontal concatenation(即沿第二个(列)维度的连接)在方括号内使用空格或逗号:

cellout = [cellint cellint];          % or ...
cellout = [cellint, cellint];         % or ...

% Functional equivalents:
cellout = horzcat(cellint, cellint);  % or ...
cellout = cat(2, cellint, cellint);

Vertical concatenation(即沿第一个(行)维度的连接)在方括号内使用分号:

cellout = [cellint; cellint];         % or ...

% Functional equivalents:
cellout = vertcat(cellint, cellint);  % or ...
cellout = cat(1, cellint, cellint);

对于沿任意维度的连接(如上面每个示例的最后一行所示),使用函数cat

cellout = cat(3, cellint, cellint);  % Would concatenate along the third dimension