How to Transpose a Matrix in C
Ever worked with matrices in math? If so, you might have come across the term ‘transpose’. But how do we achieve this in the C programming language? This article breaks down the concept of matrix transposition in C, making it easy for anyone to grasp. Curious to learn how? Let’s dive in!
Transpose of a Matrix is a process of switching its rows and columns. Transpose of a matrix changes the position of the elements of the matrix, but the matrix properties will remain the same.
In simple terms, the transpose of a matrix is interchanging rows and columns. If A is any matrix, then the transpose of the matrix is given by AT.
Must Check: What is C Programming?
Must Check: Types of Matrix
Representation
If A is any matrix of order m x n, then A’s transpose will be n x m.
A = [aij]m x n, then AT = A' = [aij]n x m.
Let’s take an example to get a better understanding of the Transpose of a Matrix.
In this article, we will discuss different methods to transpose how to transpose a matrix in C programming.
Let’s start with the nested for loop.
Transpose of a Matrix using Nested for Loop
#include<stdio.h>
int main() { int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int transpose[3][3];
// Transposing the matrix for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { transpose[j][i] = matrix[i][j]; // Swapping rows with columns } }
// Printing the transposed matrix printf("Transposed Matrix:\n"); for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { printf("%d ", transpose[i][j]); } printf("\n"); }
return 0;}
Output
In the above example, we have done the transposition only for a single matrix (i.e., we have defined the matrix). Now let’s generalize the above example. i.e., now we will take the input from the users using the same nested for loop.
#include<stdio.h>
int main() { int m, n;
// Get matrix dimensions from user printf("Enter the number of rows and columns: "); scanf("%d %d", &m, &n);
int matrix[m][n], transpose[n][m];
// Input matrix values printf("Enter matrix elements:\n"); for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { scanf("%d", &matrix[i][j]); } }
// Transposing using nested loops for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { transpose[j][i] = matrix[i][j]; } }
// Display the transposed matrix printf("Transposed Matrix:\n"); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { printf("%d ", transpose[i][j]); } printf("\n"); }
return 0;}
Must Check: Introduction to For Loops in C Programming
Now, we will discuss one another method to transpose a matrix in C programming.
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
Transpose of a Matrix using Pointers
#include<stdio.h>#include<stdlib.h>
int main() { int m, n;
// Get matrix dimensions from user printf("Enter the number of rows and columns: "); scanf("%d %d", &m, &n);
int **matrix, **transpose;
// Dynamic memory allocation matrix = (int **)malloc(m * sizeof(int *)); transpose = (int **)malloc(n * sizeof(int *)); for(int i=0; i<m; i++) { matrix[i] = (int *)malloc(n * sizeof(int)); } for(int i=0; i<n; i++) { transpose[i] = (int *)malloc(m * sizeof(int)); }
// Input matrix values printf("Enter matrix elements:\n"); for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { scanf("%d", *(matrix + i) + j); } }
// Transposing using pointers for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { *(*(transpose + j) + i) = *(*(matrix + i) + j); } }
// Display the transposed matrix printf("Transposed Matrix:\n"); for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { printf("%d ", *(*(transpose + i) + j)); } printf("\n"); }
// Free allocated memory for(int i=0; i<m; i++) { free(matrix[i]); } for(int i=0; i<n; i++) { free(transpose[i]); } free(matrix); free(transpose);
return 0;}
Must Check: Pointers in C Programming
Now, let’s see what we have done in both the above methods.
Explanation
- User Input: In both methods, we took input from the user.
- Matrix Input: We then prompt the user to input the matrix values.
- Transposition:
- In the first method, we use nested loops to transpose the matrix.
- In the second method, we utilize pointers to achieve the same.
- Display: The transposed matrix is then displayed to the user.
Note:
- Memory Allocation: In the second method, we dynamically allocate memory for the matrix and its transpose using pointers.
- Memory Deallocation: In the second method, we free the dynamically allocated memory to prevent memory leaks.
Conclusion
Transposition is a process of interchanging rows and columns. It plays an important role in computer graphics (transforming 3D models into 2D models), Data Science (data preprocessing, especially when dealing with a tabular dataset), and cryptography (some algorithms use matrix transposition in the encryption or decryption process).
There are different methods to transpose a matrix in C programming. Here, we have discussed two common methods: Nested For Loops and using pointers.
Hope you will like the article.
Keep Learning!!
Keep Sharing!!
FAQs
What is matrix transposition in C?
Matrix transposition is the process of converting all the rows of a given matrix into columns and vice versa. In C, this can be done by swapping the matrix elements matrix[i][j] with matrix[j][i] in a nested loop structure.
How do I initialize a matrix in C for transposition?
You can initialize a matrix in C using a two-dimensional array, like int matrix[rows][columns];. Fill this matrix with values before transposing.
What is the basic algorithm to transpose a matrix in C?
1. Iterating over each row and column of the matrix. 2. Swapping the elements at indices [i][j] with [j][i]. Note-This approach works well for square matrices. For non-square matrices, you may need a temporary matrix to hold the transposed values.
How can I transpose a non-square matrix in C?
For a non-square matrix, you need a separate matrix to store the transposed matrix because the dimensions will change. Initialize a new matrix with flipped dimensions (rows as columns and vice versa) and copy elements accordingly.
Are there any library functions in C to help with matrix transposition?
Standard C doesn't provide specific library functions for matrix transposition. You'll need to implement the logic manually or use third-party libraries or frameworks that offer matrix operations.
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio