Request a callback

or Chat with us on

# How to Find the Transpose of a Matrix in C

Internship Assurance
DevOps & Cloud Engineering

Various calculations and applications in the domains of mathematics and computer science depend on matrix operations. A two-dimensional (2D) matrix is a collection of numbers organised into rows and columns. Among the several operations that may be performed on a matrix is finding its transposition. In disciplines like computer graphics, data analysis, and linear algebra, the transposition of a matrix is a basic idea.

In this article, we will explore how to find the transpose of a matrix in C language. We will look into several methods for finding a matrix’s transposition, along with an explanation and some examples. But let’s first clarify what a matrix transpose in C is before we move on to the many methods. Now, let’s get going.

## What is the Transpose of a Matrix in C?

The matrix transpose operation exchanges the rows and columns of a matrix. In simple terms, if you have a matrix A with dimensions m * n, then the transposed matrix (denoted as A^T) will have dimensions n * m. The elements of A^T are formed by taking the elements of A in a specific way: the element at the i-th row and j-th column of A becomes the j-th row and i-th column of A^T. Now let us consider an example where we depict this concept symbolically using C language notations:

```(A^T)ij = Aji ```

Syntax:

```For instance, if we have a matrix Aij of size 3 * 2, then we can denote this as:  3 5 6 8 7 9 1 4 5  The transpose (A^Tji) of the Aji will be as follows: 3 8 1 5 7 4 6 9 5 ```

## Algorithm to Find the Transpose of Matrix in C

Finding the Transpose of Matrix in C is very simple and the algorithm behind this is also very easy. We will understand the algorithm in steps and see how we can implement this in C using one of the approaches that we will discuss later in this blog. Below is a detailed algorithm for creating a Finding the Transpose of Matrix in C approach:

1. Initialise the matrix:
Define the original matrix A with dimensions m * n as its size.
2. Create a new matrix:
Then, allocate the space for the transposed matrix A’ with dimensions n * m.
3. Swap rows and columns:
Next, loop through the elements of the original matrix A and assign each element to its transposed position in the matrix B. You can do swap as A[i][j] = A’[j][i]
4. Display transposed matrix:
Finally, now output the transposed matrix A’ as n * m.

### Pseudocode

Let’s now understand the pseudocode for the discussed algorithm that you can apply in C language and other languages. The pseudocode for Finding the Transpose of Matrix in C:

```START Initialize matrix A of size m * n Create matrix A’ of size n * m for i from 0 to m-1 do for j from 0 to n-1 do A’[j][i] = A[i][j] end for end for Print matrix A’ END ```

Explanation:

In this pseudocode, we have outlined the basic algorithm to create a matrix of size m * n and transpose it as n * m to create its transpose. The core of the transposition operation is performed using a nested loop. In matrix A, the outer loop iterates through each row from 0 to m – 1, whereas the inner loop iterates over each column for each row from 0 to n – 1. The element at position A[i][j] in the original matrix is transferred to location A’[j][i] in the transposed matrix at each iteration. This produces a transpose of the matrix by effectively swapping the rows and columns.

## Approaches

In C language, there are various approaches by which you can write a simple calculator program. We will discuss the finding the transpose of a matrix in C, and these are:

• Using swapping
• Using static arrays
• Using dynamic memory allocation

Let’s discuss these approaches in detail with the code examples, their explanation, and the respective output of each example.

### 1. Using Swapping

The first approach to finding a transpose of a matrix in C is to swap or interchange the two matrices of a Square matrix. A square matrix in C has rows and columns equal (m == n). By using the swapping method, we don’t have to use any extra space or memory and need not create an auxiliary matrix. This approach swaps the elements across the diagonal of the matrix.

Condition for swap:

Here, we swap elements across the diagonal using the nested loops to iterate through the upper triangular part of the matrix. For each element A[i][j] where i<j, swap it with A’[j][i].

Let’s see a detailed example now for finding the transpose of the matrix using swapping:

Code example:

```#include <stdio.h> #define N 4 void transposeMatrix(int arr[N][N]); int main() { int arr[N][N]; // User input for the matrix of size N * N printf("Enter the elements of the %d x %d matrix:n", N, N); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("Element [%d][%d]: ", i, j); scanf("%d", &arr[i][j]); } } // Printing the original matrix printf("User inputted original matrix:n"); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%d ", arr[i][j]); } printf("n"); } // Transposing the matrix using function transposeMatrix(arr); // Printing the transposed matrix printf("nTransposed Matrix:n"); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%d ", arr[i][j]); } printf("n"); } return 0; } void transposeMatrix(int arr[N][N]) { for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { int temp = arr[i][j]; arr[i][j] = arr[j][i]; arr[j][i] = temp; } } } ```

Output:

```Enter the elements of the 4 x 4 matrix: Element [0][0]: 9 4 4 2 Element [0][1]: Element [0][2]: Element [0][3]: Element [1][0]: 7 3 5 2 Element [1][1]: Element [1][2]: Element [1][3]: Element [2][0]: 8 9 6 4 Element [2][1]: Element [2][2]: Element [2][3]: Element [3][0]: 3 7 2 9 Element [3][1]: Element [3][2]: Element [3][3]:  User inputted original matrix: 9 4 4 2 7 3 5 2 8 9 6 4 3 7 2 9  Transposed Matrix: 9 7 8 3 4 3 9 7 4 5 6 2 2 2 4 9 ```

Explanation:

In this example, we have used the swapping method to find the transpose of the matrix in C. Here, we are first taking the input from the users in the form N * N (N is the size of the matrix), where they are rows and columns. We have used a nested loop to take the user input and show the user the original matrix that was inputted by them. The transpose function is used to take the matrix A and perform an in-place transpose by swapping elements across the diagonal. Here, for each element A[i][j] where i < j, it swaps it with A’[j][i]. Finally, we have printed the transposed matrix after all the swapping of elements.

### 2. Using Static Arrays

Another approach for finding the transpose of a matrix in C is using static arrays. It is a straightforward approach where the size of the matrix is known in advance. Here the arrays with the fixed size are determined at the compile time in C language. The size of the matrix is defined as rows (m) and columns (n) in the program. This approach is applied to both square and rectangular matrices.

Let’s see a detailed example now for finding the transpose of the matrix using static arrays of fixed size.

Code example:

```#include <stdio.h> // defining the m * n matrix size as 4 * 3 #define m 4 #define n 3 void transMatrix(int arr1[m][n], int arr2[n][m]); int main() { int arr1[m][n]; int arr2[n][m]; // getting the input for the matrix elements from the users as m * n printf("Enter the elements of the %d x %d matrix:n", m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("Element [%d][%d]: ", i, j); scanf("%d", &arr1[i][j]); } } // Transpose the matrix using function transMatrix(arr1, arr2); // Displaying the original matrix printf("Your original matrix is:n"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%d ", arr1[i][j]); } printf("n"); } // Displaying the transposed matrix printf("nYour transposed matrix is:n"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { printf("%d ", arr2[i][j]); } printf("n"); } return 0; } // Creating a function to transpose the matrix void transMatrix(int arr1[m][n], int arr2[n][m]) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { arr2[j][i] = arr1[i][j]; } } } ```

Output:

```Enter the elements of the 4 x 3 matrix: Element [0][0]: 8 7 9 Element [0][1]: Element [0][2]: Element [1][0]: 3 4 6 Element [1][1]: Element [1][2]: Element [2][0]: 6 3 8 Element [2][1]: Element [2][2]: Element [3][0]: 2 5 9 Element [3][1]: Element [3][2]:  Your original matrix is: 8 7 9 3 4 6 6 3 8 2 5 9  Your transposed matrix is: 8 3 6 2 7 4 3 5 9 6 8 9 ```

Explanation:

In this example, we have used the static arrays method to find the transpose of the matrix in C. Here,

• We have defined the matrix dimensions as 4 rows and 3 columns, respectively. It can be changed later on.
• The function transMatrix() is declared to transpose the matrix, which takes two 2D arrays as arguments: arr1 (the original matrix) and arr2 (the transposed matrix).
• The main function initialises a matrix arr1, and its transpose arr2 obtains user input for arr1, transposes arr1 using the transMatrix function to populate arr2, and prints both matrices in their original and transposed forms.
• The transMatrix() function is called to transpose arr1, placing the result into arr2. Finally, both matrices are printed: arr1 displays each element in its original layout, while arr2 shows each element after transposition.

### 3. Using Dynamic Memory Allocations

Dynamic memory allocation is another approach used for finding the transpose of a matrix in C. As we have seen in the static arrays approach where the size was pre-known and users can define the matrix size on their own and the size of the matrix is known at the compile time. In this approach, the matrix size is not known at the compile time but needs to be determined at the runtime.

For dynamic memory allocation, we use the concept of pointers and malloc functions. It includes the usage of malloc and free to manage memory.

Code example:

```#include <stdio.h> #include <stdlib.h>  // Creating a transpose matrix function void transMatrix(int **arr1, int **arr2, int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { arr2[j][i] = arr1[i][j]; } } }  void transMatrix(int **arr1, int **arr2, int m, int n);  // Main function int main() { int m, n;  // Getting the input for the number of m as rows and n as columns printf("Enter the number of rows: "); scanf("%d", &m); printf("Enter the number of columns: "); scanf("%d", &n);  // Dynamically allocating the memory for the original matrix int **arr1 = (int **)malloc(m * sizeof(int *)); for (int i = 0; i < m; i++) { arr1[i] = (int *)malloc(n * sizeof(int)); }  // Getting the input for the matrix elements printf("Enter the elements of the %d x %d matrix:n", m, n); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("Element [%d][%d]: ", i, j); scanf("%d", &arr1[i][j]); } }  // Dynamically allocating the memory for the transposed matrix int **arr2 = (int **)malloc(n * sizeof(int *)); for (int i = 0; i < n; i++) { arr2[i] = (int *)malloc(m * sizeof(int)); }  // Transpose the matrix transMatrix(arr1, arr2, m, n);  // Displaying the original matrix printf("nOriginal Matrix:n"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%d ", arr1[i][j]); } printf("n"); }  // Displaying the transposed matrix printf("nTransposed Matrix:n"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { printf("%d ", arr2[i][j]); } printf("n"); }  // Free the alloc memory of both arrays for (int i = 0; i < m; i++) { free(arr1[i]); } free(arr1);  for (int i = 0; i < n; i++) { free(arr2[i]); } free(arr2);  return 0; } ```

Output:

```Enter the number of rows: 3 Enter the number of columns: 3 Enter the elements of the 3 x 3 matrix: Element [0][0]: 4 5 6 Element [0][1]: Element [0][2]: Element [1][0]: 2 3 7 Element [1][1]: Element [1][2]: Element [2][0]: 7 8 9 Element [2][1]: Element [2][2]:  Original Matrix: 4 5 6 2 3 7 7 8 9  Transposed Matrix: 4 2 7 5 3 8 6 7 9 ```

Explanation:

This example demonstrates the use of the dynamic memory allocation approach in C programming to compute the matrix transpose.

• The function transMatrix() is defined for this purpose, which accepts two 2D arrays (arr1 and arr2) representing the original and transposed matrices respectively, as well as the dimensions m (rows) and n (columns).
• In this case, memory gets dynamically allocated for the matrix arr1 by user input dimensions (m rows and n columns) and similarly for the transposed matrix arr2 where the dimensions are swapped (n rows and m columns).
• The transMatrix() function is invoked to perform the transposition of arr1 into arr2; within transMatrix(), every element arr1[i][j] gets copied to arr2[j][i], which results in the transpose operation at the level of elements.
• We then print both the original and transposed matrices with their values.
• Lastly, we’ve used the free() function to release the allocated memory for both matrices, ensuring no memory leaks occur.

These are the common approaches to finding the transpose of a matrix in C. There are still approaches left that you can use to write this program. The choice between which approach to choose depends on you according to various factors like the program complexity, time constraints, or any specific requirements.

Internship Assurance
DevOps & Cloud Engineering

## Conclusion

A basic mathematical and computer science procedure, finding the transpose of a matrix has many uses. In this article, we have learned how to find the transpose of a matrix in C. We have seen different approaches, including the swapping of elements, using static arrays for fixed length, and using dynamic memory allocation methods.

Now that you know how to effectively transpose matrices in your C programs using different approaches, you can work with both fixed-size and dynamically-sized matrices. Gaining an understanding of these ideas will improve your capacity to manage matrix operations and open the door to more difficult computational assignments.

FAQs
The transpose of a matrix involves flipping the matrix over its diagonal. It can be denoted as A^T[j][i] = A[i][j], where A is the original matrix and A^T is the transposed matrix.
Yes, we can use arrays in C to find the matrix transpose. The 2D array can be used where you can store the matrix, iterate through its elements using nested loops, and then swap the elements appropriately to achieve the transpose.
Yes, you can multiply the matrices using the transpose of a matrix in C. When multiplying matrices, say A of size m * n and B of size n * r, if you wish to multiply A with the transpose of B (B^T), B must be transposed first to make it r * n, and then, normal matrix multiplication rules apply.
In C, it is easier to allocate memory dynamically when working with matrices whose size is unknown at compile time. We can use the functions like calloc() and malloc() to dynamically allocate memory for matrices. Memory for the original and transposed matrices must be allocated independently when transposing a matrix dynamically, and care must be taken to free up allocated memory to avoid memory leaks.
Large matrix transpositions can cause issues with data localization, memory management, and computational efficiency. Performance is maximised by effective transposition techniques, which reduce data movement and cache misses. And more complexities may arise when operated.

Book a free counselling session

Get tailored program recommendations

Explore industry trends and job opportunities

Popular

Data Science

Technology

Finance

Management

Future Tech

Upskill with expert articles
View all
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.

Blogs
Reviews
Events
In the News