Open In App

C++ Program To Find Transpose of a Matrix

Last Updated : 21 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, the transpose of A[][] is obtained by changing A[i][j] to A[j][i].

Example:

matrix-transpose

1. For Square Matrix 

The below program finds the transpose of A[][] and stores the result in B[][], we can change N for a different dimension.  

C++




// C++ Program to find the transpose
// of a matrix
#include <bits/stdc++.h>
using namespace std;
#define N 4
 
// This function stores transpose
// of A[][] in B[][]
void transpose(int A[][N],
               int B[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            B[i][j] = A[j][i];
}
 
// Driver code
int main()
{
    int A[N][N] = {{1, 1, 1, 1},
                   {2, 2, 2, 2},
                   {3, 3, 3, 3},
                   {4, 4, 4, 4}};
 
    int B[N][N], i, j;
 
    transpose(A, B);
 
    cout << "Result matrix is \n";
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
           cout << " " << B[i][j];
        cout <<"\n";
    }
    return 0;
}


Output

Result matrix is 
 1 2 3 4
 1 2 3 4
 1 2 3 4
 1 2 3 4

The complexity of the above method

Time Complexity: O(N*N) as two nested loops are running.

Space Complexity: O(N*N) as 2d array is created to store transpose.

2. For Rectangular Matrix

The below program finds the transpose of A[][] and stores the result in B[][]. 

C++




// C++ program to find transpose
// of a matrix
#include <bits/stdc++.h>
using namespace std;
#define M 3
#define N 4
 
// This function stores transpose
// of A[][] in B[][]
void transpose(int A[][N], int B[][M])
{
    int i, j;
    for(i = 0; i < N; i++)
        for(j = 0; j < M; j++)
            B[i][j] = A[j][i];
}
 
// Driver code
int main()
{
    int A[M][N] = {{1, 1, 1, 1},
                   {2, 2, 2, 2},
                   {3, 3, 3, 3}};
 
    // Note dimensions of B[][]
    int B[N][M], i, j;
 
    transpose(A, B);
 
    cout << "Result matrix is \n";
    for(i = 0; i < N; i++)
    {
        for(j = 0; j < M; j++)
            cout << " " << B[i][j];
             
        cout << "\n";
    }
    return 0;
}


Output

Result matrix is 
 1 2 3
 1 2 3
 1 2 3
 1 2 3

The complexity of the above method

Time Complexity: O(N*M) as two nested loops are running.

Space Complexity: O(N*M) as 2d array is created to store transpose.

3. In-Place for Square Matrix

Below is the implementation of the method:

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
#define N 4
 
// Converts A[][] to its transpose
void transpose(int A[][N])
{
    for (int i = 0; i < N; i++)
        for (int j = i+1; j < N; j++)
            swap(A[i][j], A[j][i]);
}
 
// Driver code
int main()
{
    int A[N][N] = {{1, 1, 1, 1},
                   {2, 2, 2, 2},
                   {3, 3, 3, 3},
                   {4, 4, 4, 4}};
 
    transpose(A);
 
    printf("Modified matrix is \n");
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
           printf("%d ", A[i][j]);
        printf("\n");
    }
 
    return 0;
}


Output

Modified matrix is 
1 2 3 4 
1 2 3 4 
1 2 3 4 
1 2 3 4 

The complexity of the above method

Time complexity: O(n)

  • Transpose has a time complexity of O(n + m), where n is the number of columns and m is the number of non-zero elements in the matrix. 
  • The computational time for transposing of a matrix using an identity matrix as a reference matrix is O(m*n). 
  • Suppose, if the given matrix is a square matrix, the running time will be O(n2).

Auxiliary space: O(1).



Similar Reads

Python Program to find transpose of a matrix
Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, transpose of A[][] is obtained by changing A[i][j] to A[j][i]. For Square Matrix: The below program finds transpose of A[][] and stores the result in B[][], we can change N for different dimension. C/C++ Code # Python3 Program to find # transpose of a
5 min read
Program to find transpose of a matrix
Given a matrix of size N X M, find the transpose of the matrixTranspose of a matrix is obtained by changing rows to columns and columns to rows. In other words, transpose of A[N][M] is obtained by changing A[i][j] to A[j][i]. Example: Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Approach: Follow the given ste
10 min read
Inplace (Fixed space) M x N size matrix transpose
Given an M x N matrix, transpose the matrix without auxiliary memory.It is easy to transpose matrix using an auxiliary array. If the matrix is symmetric in size, we can transpose the matrix inplace by mirroring the 2D array across it's diagonal (try yourself). How to transpose an arbitrary size matrix inplace? See the following matrix, a b c a d g
23 min read
Transpose graph
Transpose of a directed graph G is another directed graph on the same set of vertices with all of the edges reversed compared to the orientation of the corresponding edges in G. That is, if G contains an edge (u, v) then the converse/transpose/reverse of G contains an edge (v, u) and vice versa. Given a graph (represented as adjacency list), we nee
9 min read
Generate matrix from given Sparse Matrix using Linked List and reconstruct the Sparse Matrix
Given a sparse matrix mat[][] of dimensions N*M, the task is to construct and represent the original matrix using a Linked List and reconstruct the givensparse matrix. Examples: Input: mat[][] = {{0, 1, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 2, 0, 0}, {0, 3, 0, 0, 4}, {0, 0, 5, 0, 0}}Output:Linked list representation: (4, 2, 5) ? (3, 4, 4) ? (3, 1, 3) ?
19 min read
Generate a Matrix such that given Matrix elements are equal to Bitwise OR of all corresponding row and column elements of generated Matrix
Given a matrix B[][] of dimensions N * M, the task is to generate a matrix A[][] of same dimensions that can be formed such that for any element B[i][j] is equal to Bitwise OR of all elements in the ith row and jth column of A[][]. If no such matrix exists, print "Not Possible". Otherwise, print the matrix A[][]. Examples: Input: B[][] = {{1, 1, 1}
11 min read
Program to check diagonal matrix and scalar matrix
Diagonal matrix A square matrix is said to be a diagonal matrix if the elements of the matrix except the main diagonal are zero. A square null matrix is also a diagonal matrix whose main diagonal elements are zero. Examples: Input: Mat[4][4] = {{4, 0, 0, 0}, {0, 5, 0, 0}, {0, 0, 2, 0}, {0, 0, 0, 1}} Output: Yes Input: Mat[4][4] = {{6, 10, 12, 0}, {
10 min read
Program to check if a matrix is Binary matrix or not
Given a matrix, the task is to check if that matrix is a Binary Matrix. A Binary Matrix is a matrix in which all the elements are either 0 or 1. It is also called Logical Matrix, Boolean Matrix, Relation Matrix. Examples: Input: {{1, 0, 1, 1}, {0, 1, 0, 1} {1, 1, 1, 0}} Output: Yes Input: {{1, 0, 1, 1}, {1, 2, 0, 1}, {0, 0, 1, 1}} Output: No Approa
5 min read
Program to convert given Matrix to a Diagonal Matrix
Given a N*N matrix. The task is to convert the matrix to a diagonal matrix. That is to change the values of the non-diagonal elements of a matrix to 0.Diagonal-Matrix: A matrix is called a Diagonal Matrix if all the non-diagonal elements of the matrix are zero.Examples: Input : mat[][] = {{ 2, 1, 7 }, { 3, 7, 2 }, { 5, 4, 9 }} Output : {{2, 0, 7},
8 min read
C++ program to Convert a Matrix to Sparse Matrix
Given a matrix with most of its elements as 0, convert this matrix to sparse matrix in C++Examples: Input: Matrix: 0 1 1 1 2 2 2 1 3 3 2 5 4 3 4 Output: Sparse Matrix: 0 1 0 0 0 0 2 0 0 3 0 0 0 0 5 0 0 0 0 4 Explanation: Here the Sparse matrix is represented in the form Row Column Value Hence the row 0 1 1 means that the value of the matrix at row
3 min read