You can vertically concatenate arrays in MATLAB using the vertcat
function or the []
operator.
Using vertcat
Function
The vertcat
function takes two or more arrays as input and stacks them vertically, creating a new array with the combined rows.
Example:
A = [1 2; 3 4];
B = [5 6; 7 8];
C = vertcat(A, B);
disp(C);
Output:
1 2
3 4
5 6
7 8
Using []
Operator
You can also use the []
operator to concatenate arrays vertically. Simply place the arrays inside the brackets, separated by commas.
Example:
A = [1 2; 3 4];
B = [5 6; 7 8];
C = [A; B];
disp(C);
Output:
1 2
3 4
5 6
7 8
Practical Insights
- The
vertcat
function and the[]
operator achieve the same result. You can choose whichever method is more convenient for you. - Ensure that the arrays you are concatenating have the same number of columns. Otherwise, MATLAB will throw an error.