F.caren
F.caren

Reputation: 95

How to concatenate a row vector with itself in MATLAB?

I have a row vector of size <1x12582 cell>, and I want to combine it with itself to get as a result <1x25164 cell>. For example:

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

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

I have tried horzcat as follows, but it didn't give the correct result:

SS = [cellint;cellint];

Upvotes: 1

Views: 352

Answers (1)

gnovice
gnovice

Reputation: 125854

Horizontal concatenation (i.e. concatenation along the second (column) dimension) uses spaces or commas within square brackets:

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

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

Vertical concatenation (i.e. concatenation along the first (row) dimension) uses semicolons within square brackets:

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

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

For concatenation along an arbitrary dimension (as illustrated in the last line of each example above), use the function cat:

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

Upvotes: 1

Related Questions