DSA Visualizer
MATRIX

Flip a Matrix

Mirror the grid. Horizontal reverses every row; vertical reverses the order of the rows. Same shape, new positions.

Mirror the matrix: horizontally swaps left and right within each row, vertically swaps the top and bottom rows.

0
1
2
3
0
1
2
3
4
1
5
6
7
8
2
9
10
11
12
A · 3 × 4
0
1
2
3
0
·
·
·
·
1
·
·
·
·
2
·
·
·
·
A flipped left ↔ right · 3 × 4
Make an empty 3 × 4 result. A is 3 × 4.
0
ReadsReads
0
WritesWrites
12
LeftRemaining
1 / 14

Pseudocode

Flip
1B = new matrix[rows][cols]
2for i in 0 .. rows-1:
3 for j in 0 .. cols-1:
4 B[i][cols-1-j] = A[i][j]
5return B

Axis

Legend

ReadingWritingWritten

Matrix size

Want your own values? Edit this grid in the 2D Array Visualizer →

Tip: Space play/pause · ← → step

Learn flip

Flip mirrors a matrix without changing its shape. A horizontal flip reverses every row, so column j moves to column cols-1-j; a vertical flip reverses the order of the rows, so row i moves to row rows-1-i.

Transpose, rotate and flip never change a value: each one is a rule for where every cell moves, so they all cost one read and one write per cell. Compare with transpose, rotate 90° and multiply.

Two nested loops over the source, so it runs in O(rows · cols) time and needs a second matrix of the same size, unless you do it in place.

How flipping works

Flipping a matrix produces its mirror image. A horizontal flip mirrors left and right, as if a mirror stood along the right edge: the last column becomes the first, and every row is read backwards. A vertical flip mirrors top and bottom: the last row becomes the first, and each row keeps its own order.

Unlike a transpose or a rotation, a flip keeps the shape: a 3 × 4 matrix is still 3 × 4 afterwards. Use the Horizontal and Vertical toggle above to compare the two mirrors on the same grid, and watch which pointer moves backwards.

The index formulas

A horizontal flip changes only the column index. Column j moves to column cols − 1 − j:

B = new matrix[rows][cols]
for i in 0 .. rows-1:
  for j in 0 .. cols-1:
    B[i][cols-1-j] = A[i][j]
return B

A vertical flip changes only the row index, so the write becomes B[rows-1-i][j] = A[i][j]. In both cases the loops visit every cell once, one read and one write each, for rows × cols steps. The − 1 is the usual zero-based fix: with 4 columns the last column is column 3, so column 0 must land on 4 − 1 − 0 = 3.

A worked example: a 3 × 4 grid, horizontally

Each row is reversed on its own. 1 2 3 4 becomes 4 3 2 1, 5 6 7 8 becomes 8 7 6 5, and 9 10 11 12 becomes 12 11 10 9. The rows stay in the same order, so the top row is still the top row.

Take 7 at A[1][2]: it moves to column 4 − 1 − 2 = 1, so B[1][1] = 7. A vertical flip would leave it in column 2 and move it to row 3 − 1 − 1 = 1, the middle row, which is where it already is.

Flipping in place

Flips are easy to do without a second matrix. A horizontal flip is a two-pointer reverse on every row: one pointer starts at the left end, one at the right, they swap and step towards each other until they meet.

for each row of A:
  lo = 0, hi = cols-1
  while lo < hi:
    swap(row[lo], row[hi])
    lo += 1, hi -= 1

A vertical flip swaps whole rows instead: row i with row rows − 1 − i, for i up to the middle. Stop at the middle in both cases. Run the loop over every row and every pair is swapped twice, which hands you the original matrix back.

The flips also compose nicely: a horizontal flip followed by a vertical flip is a 180° rotation, and a transpose followed by a horizontal flip is a 90° clockwise rotation.

Where you'll meet it

  • Images. Flip Horizontal and Flip Vertical in any image editor are these two loops over the pixels.
  • Sprites. Games draw a character facing left by flipping the right-facing sprite instead of storing both.
  • Symmetry checks. A pattern is symmetric about an axis if flipping it about that axis changes nothing.
  • Interviews. Flipping an Image flips each row and inverts the bits in one pass.

Mistakes beginners make

  • Swapping all the way across. The in-place loop must stop at the middle, or everything swaps back.
  • Dropping the − 1. cols − j is one past the end for j = 0.
  • Mixing up the axes. Horizontal changes the column index, vertical changes the row index. The names describe the direction of the mirror, not the axis it sits on.

Every flip costs one step per cell. See how that growth compares with other algorithms: Big-O Playground →

Matrix flip questions

What is the difference between a horizontal and a vertical flip?

A horizontal flip mirrors left and right: every row is reversed, so the first column becomes the last. A vertical flip mirrors top and bottom: the order of the rows is reversed, so the first row becomes the last. Neither changes the shape of the matrix.

How do you flip a matrix in place?

Horizontally, reverse each row with two pointers that swap and move inwards until they meet. Vertically, swap row i with row rows − 1 − i for the first half of the rows. Both need only a temporary variable per swap.

Is flipping twice the same as rotating 180 degrees?

Yes, if you flip both ways. A horizontal flip followed by a vertical flip, in either order, gives the same result as two 90° rotations. Flipping the same way twice gives the original matrix back.

What is the time complexity of flipping a matrix?

O(rows × cols) when copying into a new matrix, one read and one write per cell. In place it is about half that many swaps, but still O(rows × cols) overall.

Open in DSA Visualizer ↗