How to Transpose a Matrix in C

How to Transpose a Matrix in C

4 mins read71 Views Comment
Updated on Jul 11, 2024 11:10 IST

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 in C

 

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.

2023_08_transpose-of-matrix-example.jpg

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;
}
Copy code

Output

2023_08_example-transpose-of-a-matrix-1.jpg

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;
}
Copy code
Matrix Multiplication in C
Matrix Multiplication: A Beginner’s Guide to Understand and Implement
All About Skew Symmetric Matrix

Now, we will discuss one another method to transpose a matrix in C programming.

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4 K
2 months
– / –
40 hours
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
1 month
– / –
2 months
– / –
50 hours
5.5 K
140 hours

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;
}
Copy code
All about Symmetric Matrix
Difference between Eigenvalue and Eigenvector
3 Important Types of Vector Norm Used in Machine Learning

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!!

Hello World Program in C
10 Must-Read C Programming Books of All Time
C programming examples 
What is Interpreter: Types, Advantages and Disadvantages
Learn About Array of Structure in C
Explore strtok() Function in C
Guide to tolower() Function in C with Examples
How to Reverse an Array in C
Learn to Implement strstr() Function in C
Concatenate Strings with strcat() Function in C
Compare Strings with strcmp Function in C
Explore toupper() Function in C with Examples
All About Strlen() Function in C
Understanding Strchr() Function in C
strcpy() Function in C

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.

About the Author