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
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 horizontallydisp(C)disp('Using square brackets:')D = [A B];disp(D)
Explanation
Lines 1–2: Define two matrices
AandBwith dimension. Line 3: It displays a message indicating that the
horzcat()function is about to be used.Line 4: It concatenates matrices
AandBhorizontally using thehorzcat()function, meaning that the columns ofBare appended to the columns ofAto form matrixC.Line 5: It displays the concatenated matrix
Cusing thedisp()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
AandBhorizontally. It creates a new matrixD.Line 8: It displays the concatenated matrix
Dusing thedisp()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
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 horizontallydisp(C)disp('Using square brackets:')D = [A; B];disp(D)
Explanation
Lines 1–2: Define two matrices
AandBwith dimension. Line 3: It displays a message indicating that the
vertcat()function is about to be used.Line 4: It concatenates matrices
AandBvertically using thevertcat()function, meaning that the rows ofBare appended below the rows ofAto form matrixC.Line 5: It displays the concatenated matrix
Cusing thedisp()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
AandBvertically. It creates a new matrixD.Line 8: It displays the concatenated matrix
Dusing thedisp()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
AandBwith dimension. Line 3: It concatenates matrices
AandBalong dimension, meaning it stacks BbelowA.Line 4: The resulting matrix
Cis displayed, showing the concatenated matrix where the rows ofBare added below the rows ofA.
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
AandBwith dimension. Line 3: It concatenates matrices
AandBalong dimension, meaning it stacks BbesideA.Line 4: The resulting matrix
Cis displayed, showing the concatenated matrix where the columns ofBare added beside the columns ofA.
This will result in a matrix C with size horzcat(A, B).
Free Resources