Matlab concatenate vectors: vertically, horizontally

Matlab concatenate vectors: vertically, horizontally

The operation of concatenating vectors or matrices under MATLAB is defined as a combination of the variables in a single vector or matrix. There are two types of concatenation operation: horizontal and vertical. For horizontal concatenation, two variables should have the same number of rows. In case of vertical concatenation, two variables should have the same number of columns. Every available variable cannot be concatenated.

Concatenation

Concatenation of variables in Matlab is defined as the combination of these variables in a single vector or matrix. Concatenation operations with multiple variables in MATLAB software are possible as well, as one vector can be concatenated with the transpose of the same vector.

Example:

A =
2     3     4
     1     2     5
     0     2     7

With:

Y =
9     6     8
     5     6     2
     3     2     1

Concatenation of rows:

Result =
2     3     4     9     6     8
     1     2     5     5     6     2
     0     2     7     3     2     1

Concatenation of columns:

Result =
2     3     4
     1     2     5
     0     2     7
     9     6     8
     5     6     2
     3     2     1

Horizontal concatenation

Horizontal concatenation can be done using vectors, matrices or simple variables. The two variables (X and Y) must have the same number of rows. The code is as follows:

Result = [X Y] 

Vertical concatenation

Vertical concatenation can be done using vectors, matrices or simple variables. The two variables (X and Y) must have the same number of columns. The code is as follows:

Result = [X; Y] 

Further calculations

  • Option 1

You can concatenate a row with a column, transposing a row as a column or viceversa using the command:

Result = [X '  Y]
  • Option 2

You can extend this principle to cover multiple variables. For example using the command:

Result = [[A;B] C;D E]
Any more software questions? check out our forum!
Around the same subject

Software