The function `flip` reverses the order of elements in a matrix.

## Preparation

Create two matrices using the following code:

``````A = 1:6;
B = reshape(A,2,3);``````

## Syntax (I)

`B = flip(A)` reverses the order of the elements in `A`, and stores the result in `B`. If `A` is a vector, `flip` reverses the order of its elements. If `A` is a matrix, `flip` reverses the order of its rows.

``````% Elements of A in reverse order
resultExample1 = flip(A);``````
``````% Rows of B in reverse order
resultExample2 = flip(B);``````

## Syntax (II)

`B = flip(A,dim)` reverses the order of the elements in `A` along the `dim` dimension, and stores the result in `B`. If `dim` equals 1, `flip` reverses the order of rows. If `dim` equals 2, `flip` reverses the order of columns.

``````% Rows of B in reverse order
resultExample3 = flip(B,1);``````
``````% Columns of B in reverse order
resultExample4 = flip(B,2);``````

I recommend the following books to learn more on matrix manipulation in MATLAB.

1. This book was the winner of a Textbook Excellence Award. The second chapter explains how to perform a wide list of operations on matrices.

2. MATLAB for Engineers (5th Edition)

This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. Matrix manipulation is explained in the fourth chapter.

I also recommend the following pages in the MATLAB help documentation, and the other tutorials in this series.

``````doc flip;
doc fliplr;
doc flipud;``````

## Source code

I hope you have learned how to reverse a 2D matrix in MATLAB. The source code developed in this tutorial is available at this page.