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.
We can concatenate two matrices horizontally, i.e., adding columns, using the horzcat()
function, or using square brackets[]
.
The basic syntax of the horzcat()
function is as follows:
horzcat(A, B)
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 horizontallydisp(C)disp('Using square brackets:')D = [A B];disp(D)
Lines 1–2: Define two matrices A
and B
with dimension
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.
We can concatenate two matrices vertically, i.e., adding rows, using the vertcat()
function, or square brackets []
.
The basic syntax of the vertcat()
function is as follows:
vertcat(A, B)
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 horizontallydisp(C)disp('Using square brackets:')D = [A; B];disp(D)
Lines 1–2: Define two matrices A
and B
with dimension
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.
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
Line 3: It concatenates matrices A
and B
along dimensionB
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 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
Line 3: It concatenates matrices A
and B
along dimensionB
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 horzcat(A, B)
.