lettura simple

Flipping a Matrix in Matlab

In this lesson, I'll show you how to flip a matrix horizontally and vertically using Matlab.

What does flipping a matrix mean? It means changing the arrangement of rows and columns through a reflection of the matrix about an axis. For example, this matrix is horizontally reflected, from left to right, because the order of columns is inverted. The first column on the left becomes the last column on the right and so on.
matrix reflection
This other matrix is vertically reflected from top to bottom because the order of rows is inverted. The first row at the top becomes the last row at the bottom and so on.
vertical reflection

Here's a practical example.

Create a square matrix with three rows and three columns.

>> M=[1 2 3; 4 5 6; 7 8 9]
M =
1 2 3
4 5 6
7 8 9

To flip the matrix horizontally from right to left, type the function fliplr(M)

>> fliplr(M)
ans =
3 2 1
6 5 4
9 8 7

This function reverses the arrangement of the matrix columns.

The third column becomes the first and vice versa.

Note. The fliplr command means "flip from left to right". The term flip means reflection while lr is the English abbreviation for left-right.

o flip the matrix vertically from top to bottom, type the function flipud(M)

>> flipud(M)
ans =
7 8 9
4 5 6
1 2 3

This command reverses the order of the matrix rows.

The first row at the top becomes the last row at the bottom and vice versa.

Note. The flipud command means "flip from top to bottom". The term flip means reflection while ud is the English abbreviation for up-down.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin