Transpose of a Matrix in C++
Transpose of a Matrix in C++ (4 Programs With Output).
1#include <iostream>2using namespace std;34int 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];89 // Calculate transpose10 for (int i = 0; i < rows; i++) {11 for (int j = 0; j < cols; j++) {12 transpose[j][i] = matrix[i][j];13 }14 }1516 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 }2324 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 }3132 return 0;33}
Output
Original matrix: 1 2 3 4 5 6 7 8 9 Transpose matrix: 1 4 7 2 5 8 3 6 9
What's going on
The program transposes a matrix by swapping rows and columns. For example:
1 2 3
4 5 6
7 8 9
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
This header provides:
1#include <iostream>
Understanding Matrix Transposition
Definition
:
If A is a matrix, then A^T (transpose) is obtained by:
Mathematical Notation
:
If A[i][j] is an element at row i, column j, then:
A^T[j][i] = A[i][j]
Key Property
:
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];
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
:
Step-by-step
(for 3×3 matrix):
Iteration 1:
i=0, j=0
Iteration 2:
i=0, j=1
Iteration 3:
i=0, j=2
Iteration 4:
i=1, j=0
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 << endl;
}
1cout << matrix[i][j] << " ";
Transpose Matrix:
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
}
cout << endl;
}
Note: For transpose, we use cols for outer loop and rows for inner loop (dimensions are swapped).
1cout << transpose[i][j] << " ";
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]);
}
}
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];
}
}
}
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];
}
}
}
1return result;
Understanding the Index Swap
Key Insight
:
The transpose operation swaps the indices:
Why This Works
:
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
:
Memory Requirements
:
Index Bounds
:
Common Use Cases
Linear Algebra
:
Computer Graphics
:
Data Science
:
return 0;
This ends the program successfully.