serve as the matrix concatenation operator in MATLAB. Use a comma or a space to separate elements horizontally, and a semicolon to separate elements vertically.
A = [1, 2; 3, 4];
A = 1 2 3 4
 operator only generates 2D matrices and the result must have a regular shape, so (Fig. 1):
- If you concatenate two or more matrices horizontally, all they must have the same number of rows
- If you concatenate two or more matrices vertically, all they must have the same number of columns
 operator to concatenate matrices of any data type, but always guarantee that the dimensions and types match.
% Concatenate four 1-by-1 matrices B = [5, 6; 7, 8];
B = 5 6 7 8
% Horizontal concatenation of A and B resultExample2 = [A,B];
resultExample2 = 1 2 5 6 3 4 7 8
% Vertical concatenation of A and B resultExample3 = [A;B];
resultExample3 = 1 2 3 4 5 6 7 8
I recommend the following books to learn more on matrix concatenation in MATLAB:
This book was the winner of a 2017 Textbook Excellence Award. The second chapter explains the concatenation operator and the colon operator, among other topics on matrix manipulation.
MATLAB for Engineers (5th Edition)
This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. The fourth chapter is completely dedicated to matrix manipulation.
I also recommend the following pages in the MATLAB help documentation, and the other tutorials in this series.
doc cat; doc horzcat; doc vertcat;
- MATLAB Concatenation Operator Explained
- How to Create Numeric Sequences Using the Colon Operator in MATLAB
- MATLAB Colon Operator in Matrix Indexing Expressions
- Multiline Statements in MATLAB
- Create a matrix
Xcontaining in the first row the even numbers in the interval \([1,10]\), and in the second row the odd numbers in such interval
- Create a row vector
Ycontaining the multiples of 5 in the interval \([5,25]\), and concatenate it with the matrix
% Clause 1 X = [2, 4, 6, 8, 10; 1, 3, 5, 7, 9]; % Clause 2 Y = [5, 10, 15, 20, 25]; R = [X;Y]; % A more elegant way X = [2:2:10; 1:2:10]; Y = 5:5:25; R = [X;Y];
I hope you have learned how to use the concatenation operator in MATLAB. The source code developed in this tutorial is available at this page.