Flipping a matrix in Matlab

Matlab and Octave both provide functions to flip matrices with ease.

To flip a matrix horizontally, mirroring it along the y-axis (from right to left), I use the fliplr() function:

fliplr(matrix)

For a vertical flip along the x-axis (from bottom to top), the flipud() function is ideal:

flipud(matrix)

Example

Let's walk through a practical example.

To start, I'll define a matrix and assign it to a variable:

M = [1,2;3,4]

This matrix is 2x2:

$$ M = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} $$

To reverse the columns in M, I simply use the fliplr(M) command:

M2 = fliplr(M)

This gives a new matrix, M2, where M has been flipped horizontally, with columns reversed:

$$ M2 = \begin{pmatrix} 2 & 1 \\ 4 & 3 \end{pmatrix} $$

Similarly, to reverse the row order in M, I can use the flipud(M) function, which flips the matrix vertically:

M3 = flipud(M)

Now, M3 is the vertically flipped version of M, with rows reordered from bottom to top:

$$ M3 = \begin{pmatrix} 3 & 4 \\ 1 & 2 \end{pmatrix} $$

 
 

Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

FacebookTwitterLinkedinLinkedin
knowledge base

Matrix in Matlab