Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Transpose of a Matrix in C++

Find the transpose of a 3×3 matrix in C++ by copying matrix[i][j] into transpose[j][i]. Shows how rows become columns and how to handle non-square matrices.

IntermediateArray Operations ProgramsExample 5 of 7
transpose-of-a-matrix.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int rows = 3, cols = 3;
6 int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
7 int transpose[3][3];
8
9 // Calculate transpose
10 for (int i = 0; i < rows; i++) {
11 for (int j = 0; j < cols; j++) {
12 transpose[j][i] = matrix[i][j];
13 }
14 }
15
16 cout << "Original matrix:" << endl;
17 for (int i = 0; i < rows; i++) {
18 for (int j = 0; j < cols; j++) {
19 cout << matrix[i][j] << " ";
20 }
21 cout << endl;
22 }
23
24 cout << "Transpose matrix:" << endl;
25 for (int i = 0; i < cols; i++) {
26 for (int j = 0; j < rows; j++) {
27 cout << transpose[i][j] << " ";
28 }
29 cout << endl;
30 }
31
32 return 0;
33}

Sample Output

1 4 7

Original column 0 as a row.

In this one

  • What a transpose is
  • How swapping the index order flips a matrix
  • Why a separate result array is used
  • How dimensions change for non-square matrices

How it works

  1. 1

    Start with 1–9 in a 3×3 grid.

  2. 2

    For each i, j copy matrix[i][j] to transpose[j][i].

  3. 3

    Print the original.

  4. 4

    Print the transpose: 1 4 7 / 2 5 8 / 3 6 9.

Edge cases

Non-square matrix

Use a 2×3 matrix.

The transpose must be declared 3×2. Keeping it the same shape writes outside the array.

In-place transpose

Swap matrix[i][j] and matrix[j][i] for all i, j.

Every pair is swapped twice and nothing changes. In place, loop only over j > i.

What's going on

The program transposes a matrix by swapping rows and columns. For example:

Original matrix (3x3):

1 2 3

4 5 6

7 8 9

Transpose matrix (3x3):

1 4 7

2 5 8

3 6 9

The first row becomes the first column, the second row becomes the second column, and so on.

Header File Used

#include <iostream>

This header provides:

cout for displaying output
cin for taking input from the user

Understanding Matrix Transposition

Definition:

If A is a matrix, then A^T (transpose) is obtained by:

Converting rows to columns
Converting columns to rows

Mathematical Notation:

If A[i][j] is an element at row i, column j, then:

A^T[j][i] = A[i][j]

Key Property:

If original matrix is m×n, transpose is n×m
For square matrices (m = n), size remains the same

Declaring Variables

The program declares:

int rows = 3, cols = 3;

int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int transpose[3][3];

rows and cols store the dimensions (3×3 in this case).
matrix stores the original 3×3 matrix.
transpose stores the transposed matrix.

The Transposition Algorithm

The core algorithm uses nested loops:

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

transpose[j][i] = matrix[i][j];

}

}

How it works:

Outer loop (i) iterates through rows of original matrix.
Inner loop (j) iterates through columns of original matrix.
transpose[j][i] = matrix[i][j] swaps row and column indices.

Step-by-step (for 3×3 matrix):

Iteration 1: i=0, j=0

transpose[0][0] = matrix[0][0] = 1

Iteration 2: i=0, j=1

transpose[1][0] = matrix[0][1] = 2

Iteration 3: i=0, j=2

transpose[2][0] = matrix[0][2] = 3

Iteration 4: i=1, j=0

transpose[0][1] = matrix[1][0] = 4

And so on...

Visual Representation:

Original: Transpose:

[0][0] [0][1] [0][2] [0][0] [1][0] [2][0]

[1][0] [1][1] [1][2] -> [0][1] [1][1] [2][1]

[2][0] [2][1] [2][2] [0][2] [1][2] [2][2]

Displaying the Matrices

The program displays both matrices:

Original Matrix:

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

cout << matrix[i][j] << " ";

}

cout << endl;

}

Transpose Matrix:

for (int i = 0; i < cols; i++) {

for (int j = 0; j < rows; j++) {

cout << transpose[i][j] << " ";

}

cout << endl;

}

Note: For transpose, we use cols for outer loop and rows for inner loop (dimensions are swapped).

Other Methods (Mentioned but not shown in code)

Method 2: In-Place Transpose (Square Matrices Only)

for (int i = 0; i < rows; i++) {

for (int j = i + 1; j < cols; j++) {

swap(matrix[i][j], matrix[j][i]);

}

}

Only works for square matrices (rows = cols).
Modifies original matrix without creating new one.
More memory-efficient.

Method 3: Using Functions

void transposeMatrix(int matrix[][3], int transpose[][3], int rows, int cols) {

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

transpose[j][i] = matrix[i][j];

}

}

}

Encapsulates logic in a function.
Makes code reusable and modular.

Method 4: Using Vectors

vector<vector<int>> transpose(vector<vector<int>>& matrix) {

int rows = matrix.size();

int cols = matrix[0].size();

vector<vector<int>> result(cols, vector<int>(rows));

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

result[j][i] = matrix[i][j];

}

}

return result;

}

Uses dynamic vectors instead of arrays.
More flexible for variable-sized matrices.

Understanding the Index Swap

Key Insight:

The transpose operation swaps the indices:

Original: matrix[row][column]
Transpose: transpose[column][row]

Why This Works:

Element at row 0, column 1 in original → row 1, column 0 in transpose
Element at row 1, column 0 in original → row 0, column 1 in transpose
This effectively swaps rows and columns

When to Use Each Method

Nested Loops with New Matrix: Best for learning - clear and straightforward.
In-Place Transpose: Best for square matrices when memory is limited.
Functions: Best for code organization - reusable and modular.
Vectors: Best for dynamic matrices - flexible and modern C++.

Best Practice: Use nested loops for learning, functions for production code, vectors for dynamic sizes.

Important Considerations

Square vs Rectangular Matrices:

Square matrices (m = n): Transpose has same dimensions.
Rectangular matrices (m ≠ n): Transpose has swapped dimensions.
Example: 2×3 matrix → 3×2 transpose

Memory Requirements:

Creating new transpose matrix requires extra memory.
For large matrices, consider in-place transpose (if square).

Index Bounds:

Always ensure indices are within bounds.
transpose[j][i] requires j < rows and i < cols of original.

Common Use Cases

Linear Algebra:

Matrix operations and transformations.
Solving systems of equations.
Eigenvalue calculations.

Computer Graphics:

Coordinate transformations.
3D rotations and projections.
Matrix transformations.

Data Science:

Data manipulation and analysis.
Feature transformations.
Machine learning algorithms.

return 0;

This ends the program successfully.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Element at matrix[0][2] = 3 moves to:

Pick an answer to check it.

Question 2

Transpose of a 2×4 matrix has shape:

Pick an answer to check it.

Questions people ask

What stays the same in a transpose?

The diagonal: matrix[i][i].

Where is transpose used?

Linear algebra, image rotation, and switching rows and columns in tables.

Why print with i < cols?

The transpose has cols rows, so the loops swap their limits.