Matrix concatenation is the process of joining one or more matrices to make a new matrix.

## [] operator

The brackets [] 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];
Best practices prefer the comma to separate elements horizontally.

The [] operator only generates 2D matrices and the result must have a regular shape, so (Fig. 1):

1. If you concatenate two or more matrices horizontally, all they must have the same number of rows
2. If you concatenate two or more matrices vertically, all they must have the same number of columns

## Examples

Use the [] 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];
% Horizontal concatenation of A and B
resultExample2 = [A,B];
% Vertical concatenation of A and B
resultExample3 = [A;B];

I recommend the following books to learn more on matrix concatenation in MATLAB:

1. This book was the winner of a Textbook Excellence Award. The second chapter explains the concatenation operator and the colon operator, among other topics on matrix manipulation.

2. 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;

## Exercise

1. Create a matrix X containing in the first row the even numbers in the interval $$[1,10]$$, and in the second row the odd numbers in such interval
2. Create a row vector Y containing the multiples of 5 in the interval $$[5,25]$$, and concatenate it with the matrix X

## Source code

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.