How to concatenate two matrices in MATLAB

Matrix concatenation in MATLAB involves combining or merging two matrices. This process allows for the expansion of matrix dimensions and facilitates diverse data manipulations. In MATLAB, we can concatenate two matrices in several ways, including the following:

  • Horizontal way: It will add the matrix next to the other matrix. It’ll increase the number of columns.

  • Vertical way: It will add the matrix below the other matrix. It’ll increase the number of rows.

  • Specific dimensions: It will add the matrix in either of the ways mentioned above.

Horizontal concatenation

We can concatenate two matrices horizontally, i.e., adding columns, using the horzcat() function, or using square brackets[].

Syntax

The basic syntax of the horzcat() function is as follows:

horzcat(A, B)

Parameters

AA and BB are two matrices, which we want to concatenate horizontally.

Visual representation of the horzcat() function
Visual representation of the horzcat() function

Example

Let’s now see the demonstration of the function:

A = [10 20; 30 40];
B = [50 60; 70 80];
disp('Using horzcat() function:')
C = horzcat(A, B); % Concatenate A and B horizontally
disp(C)
disp('Using square brackets:')
D = [A B];
disp(D)

Explanation

  • Lines 1–2: Define two matrices A and B with dimension 2×22 \times 2.

  • Line 3: It displays a message indicating that the horzcat() function is about to be used.

  • Line 4: It concatenates matrices A and B horizontally using the horzcat() function, meaning that the columns of B are appended to the columns of A to form matrix C.

  • Line 5: It displays the concatenated matrix C using the disp() function.

  • Line 6: It displays a message indicating that square brackets will be used for concatenation.

  • Line 7: It uses square brackets to concatenate matrices A and B horizontally. It creates a new matrix D.

  • Line 8: It displays the concatenated matrix D using the disp() function.

Vertical concatenation

We can concatenate two matrices vertically, i.e., adding rows, using the vertcat() function, or square brackets [].

Syntax

The basic syntax of the vertcat() function is as follows:

vertcat(A, B)

Parameters

AA and BB are two matrices, which we want to concatenate vertically.

Visual representation of the vertcat() function
Visual representation of the vertcat() function

Example

Let’s now see the demonstration of the function:

A = [10 20; 30 40];
B = [50 60; 70 80];
disp('Using horzcat() function:')
C = vertcat(A, B); % Concatenate A and B horizontally
disp(C)
disp('Using square brackets:')
D = [A; B];
disp(D)

Explanation

  • Lines 1–2: Define two matrices A and B with dimension2×22 \times 2.

  • Line 3: It displays a message indicating that the vertcat() function is about to be used.

  • Line 4: It concatenates matrices A and B vertically using the vertcat() function, meaning that the rows of B are appended below the rows of A to form matrix C.

  • Line 5: It displays the concatenated matrix C using the disp() function.

  • Line 6: It displays a message indicating that square brackets will be used for concatenation.

  • Line 7: It uses square brackets to concatenate matrices A and B vertically. It creates a new matrix D.

  • Line 8: It displays the concatenated matrix D using the disp() function.

Along the specified dimension

We can concatenate two matrices along a specified dimension using the cat() function.

A = [10 20; 30 40];
B = [50 60; 70 80];
C = cat(1, A, B); % Concatenate A and B along dimension 1 (rows)
disp(C)
  • Lines 1–2: Here, we define two matrices A and B with dimension 2×22 \times 2.

  • Line 3: It concatenates matrices A and B along dimension11, meaning it stacks B below A.

  • Line 4: The resulting matrix C is displayed, showing the concatenated matrix where the rows of B are added below the rows of A.

This will result in a matrix C with size 4×24 \times 2, which is the same as vertcat(A, B).

Similarly, we can perform concatenation along the second dimension (columns).

A = [10 20; 30 40];
B = [50 60; 70 80];
C = cat(2, A, B); % Concatenate A and B along dimension 2 (columns)
disp(C)
  • Lines 1–2: Here, we define two matrices A and B with dimension 2×22 \times 2.

  • Line 3: It concatenates matrices A and B along dimension22, meaning it stacks B beside A.

  • Line 4: The resulting matrix C is displayed, showing the concatenated matrix where the columns of B are added beside the columns of A.

This will result in a matrix C with size 2×42 \times 4, the same as horzcat(A, B).

Copyright ©2024 Educative, Inc. All rights reserved