- What is matrix transpose and why it is used
- How to transpose a matrix using a 2D array
- How to handle both square and rectangular matrices
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to transpose a matrix.
The transpose of a matrix is obtained by interchanging its rows and columns. If a matrix has dimensions r Ć c, its transpose will have dimensions c Ć r.
š” Key Point: In a transposed matrix, the element at position (i, j) in the original matrix moves to position (j, i) in the transposed matrix.
Matrix transposition is used in many real-world applications, such as:
- Image Processing: Rotating images (transpose + flip)
- Data Science: Converting data format (rows to columns)
- Computer Graphics: Matrix transformations
- Linear Algebra: Solving systems of equations
Visual Example
For a 2Ć3 matrix:
Original Matrix (2Ć3):
[1 2 3] [4 5 6]
Transposed Matrix (3Ć2):
[1 4] [2 5] [3 6]
C Program to Transpose a Matrix
#include <stdio.h>
int main() {
int rows, cols, i, j;
// Ask user for matrix dimensions
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
// Declare original and transposed matrices
int matrix[rows][cols];
int transpose[cols][rows];
// Read elements into the original matrix
printf("\nEnter %d elements:\n", rows * cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("matrix[%d][%d] = ", i, j);
scanf("%d", &matrix[i][j]);
}
}
// Transpose the matrix (swap rows and columns)
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Display original matrix
printf("\n=== Original Matrix (%dĆ%d) ===\n", rows, cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
// Display transposed matrix
printf("\n=== Transposed Matrix (%dĆ%d) ===\n", cols, rows);
for(i = 0; i < cols; i++) {
for(j = 0; j < rows; j++) {
printf("%d\t", transpose[i][j]);
}
printf("\n");
}
return 0;
}
Sample Output
Enter the number of rows: 2 Enter the number of columns: 3 Enter 6 elements: matrix[0][0] = 1 matrix[0][1] = 2 matrix[0][2] = 3 matrix[1][0] = 4 matrix[1][1] = 5 matrix[1][2] = 6 === Original Matrix (2Ć3) === 1 2 3 4 5 6 === Transposed Matrix (3Ć2) === 1 4 2 5 3 6
Example with a Square Matrix:
Enter the number of rows: 3 Enter the number of columns: 3 Enter 9 elements: matrix[0][0] = 1 matrix[0][1] = 2 matrix[0][2] = 3 matrix[1][0] = 4 matrix[1][1] = 5 matrix[1][2] = 6 matrix[2][0] = 7 matrix[2][1] = 8 matrix[2][2] = 9 === Original Matrix (3Ć3) === 1 2 3 4 5 6 7 8 9 === Transposed Matrix (3Ć3) === 1 4 7 2 5 8 3 6 9
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int rows, cols;ā dimensions of the matrixint i, j;ā loop counters
- Declare Matrices:
int matrix[rows][cols];ā original matrixint transpose[cols][rows];ā transposed matrix (dimensions are swapped)
- Read Matrix: Uses nested loops to read elements into the original matrix.
- Transpose Operation: The nested loops iterate through the original matrix and assign
matrix[i][j]totranspose[j][i], effectively swapping rows and columns. - Display Matrices: Prints both the original and transposed matrices.
- Return:
return 0;indicates successful program execution.
š Note: The transposed matrix has swapped dimensions. If the original matrix is rows Ć cols, the transposed matrix is cols Ć rows.
Algorithm to Transpose a Matrix
Step-by-step algorithm:
- Start
- Read the number of rows (r) and columns (c)
- Read r Ć c elements into the matrix
- Create a transpose matrix of size c Ć r
- For
i = 0tor-1:- For
j = 0toc-1:transpose[j][i] = matrix[i][j]
- For
- Print the original matrix
- Print the transposed matrix
- End
In-Place Transpose for Square Matrices (No Extra Space)
For square matrices, we can transpose in-place without using an extra matrix:
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
int matrix[n][n];
printf("\nEnter %d elements:\n", n * n);
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
// In-place transpose for square matrix
for(i = 0; i < n; i++) {
for(j = i + 1; j < n; j++) {
// Swap matrix[i][j] with matrix[j][i]
temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
printf("\n=== Transposed Matrix (In-Place) ===\n");
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Sample Output:
Enter the size of the square matrix: 3 Enter 9 elements: 1 2 3 4 5 6 7 8 9 === Transposed Matrix (In-Place) === 1 4 7 2 5 8 3 6 9
How In-Place Transpose Works
- Only iterates over the upper triangle of the matrix (i < j)
- Swaps
matrix[i][j]withmatrix[j][i] - The diagonal elements (i == j) remain unchanged
- Time Complexity: O(n²)
- Space Complexity: O(1) ā no extra matrix needed
Time and Space Complexity
| Method | Time Complexity | Space Complexity |
|---|---|---|
| Using Extra Matrix | O(r Ć c) | O(r Ć c) |
| In-Place (Square Only) | O(n²) | O(1) |
š» Practice Exercise
Challenge 1: Write a program to transpose a matrix without using a second array (for any matrix, not just square).
Challenge 2: Transpose a matrix and then print the sum of the transposed matrix.
š Click to Show Solution for Challenge 1
#include <stdio.h>
int main() {
int rows, cols, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols];
printf("\nEnter %d elements:\n", rows * cols);
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Display transpose by printing columns as rows
printf("\n=== Transposed Matrix (%dĆ%d) ===\n", cols, rows);
for(i = 0; i < cols; i++) {
for(j = 0; j < rows; j++) {
printf("%d\t", matrix[j][i]);
}
printf("\n");
}
return 0;
}
Frequently Asked Questions
1. What is the transpose of a matrix?
The transpose of a matrix is obtained by interchanging rows and columns. The element at position (i, j) in the original matrix moves to position (j, i) in the transposed matrix.
2. How do you transpose a matrix in C?
Use nested loops to copy matrix[i][j] to transpose[j][i]. The transposed matrix will have dimensions swapped (columns Ć rows).
3. Can I transpose a matrix without using extra space?
Yes, for square matrices, you can transpose in-place by swapping elements across the diagonal. For rectangular matrices, you need extra space.
4. What happens to the dimensions when a matrix is transposed?
If the original matrix is r Ć c, the transposed matrix is c Ć r. The number of rows and columns are swapped.
5. What is the use of matrix transposition in real life?
Matrix transposition is used in image processing (rotating images), data science (pivot tables), computer graphics, and solving systems of linear equations.
š” Tip: Transposing a matrix twice returns the original matrix. This property is useful in many mathematical operations.