Program to find transpose of a matrix - GeeksforGeeks

29 Aug, 2021
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
Java
Python3
C#
PHP
Javascript
// C++ Program to find// 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 codeint 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;} // This code is contributed by shivanisinghss2110
// C Program to find// transpose of a matrix#include <stdio.h>#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];} 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);     printf("Result matrix is \n");    for (i = 0; i < N; i++)    {        for (j = 0; j < N; j++)           printf("%d ", B[i][j]);        printf("\n");    }     return 0;}
// Java Program to find// transpose of a matrix class GFG{    static final int N = 4;         // This function stores transpose    // of A[][] in B[][]    static void transpose(int A[][], int B[][])    {        int i, j;        for (i = 0; i < N; i++)            for (j = 0; j < N; j++)                B[i][j] = A[j][i];    }         // Driver code    public static void main (String[] args)    {        int A[][] = { {1, 1, 1, 1},                      {2, 2, 2, 2},                      {3, 3, 3, 3},                      {4, 4, 4, 4}};             int B[][] = new int[N][N], i, j;             transpose(A, B);             System.out.print("Result matrix is \n");        for (i = 0; i < N; i++)        {            for (j = 0; j < N; j++)            System.out.print(B[i][j] + " ");            System.out.print("\n");        }    }} // This code is contributed by Anant Agarwal.
# Python3 Program to find# transpose of a matrix N = 4  # This function stores# transpose of A[][] in B[][] def transpose(A,B):     for i in range(N):        for j in range(N):            B[i][j] = A[j][i] # driver codeA = [ [1, 1, 1, 1],    [2, 2, 2, 2],    [3, 3, 3, 3],    [4, 4, 4, 4]]    B = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] # To store result transpose(A, B)  print("Result matrix is")for i in range(N):    for j in range(N):        print(B[i][j], " ", end='')    print()     # This code is contributed# by Anant Agarwal.
// C# Program to find// transpose of a matrixusing System; class GFG{    static int N = 4;         // This function stores transpose    // of A[][] in B[][]    static void transpose(int [,]A, int [,]B)    {        int i, j;        for (i = 0; i < N; i++)            for (j = 0; j < N; j++)                B[i,j] = A[j,i];    }         // Driver code    public static void Main ()    {        int [,]A = { {1, 1, 1, 1},                     {2, 2, 2, 2},                     {3, 3, 3, 3},                     {4, 4, 4, 4}};             int [,]B = new int[N,N];                 // Function calling        transpose(A, B);             Console.Write("Result matrix is \n");        for (int i = 0; i < N; i++)        {            for (int j = 0; j < N; j++)            Console.Write(B[i,j] + " ");            Console.Write("\n");        }    }} // This code is contributed by nitin mittal.
<?php // This function stores transpose// of A[][] in B[][]function transpose(&$A, &$B){    $N = 4;    for ($i = 0; $i < $N; $i++)        for ($j = 0; $j < $N; $j++)            $B[$i][$j] = $A[$j][$i];} // Driver code$A = array(array(1, 1, 1, 1),           array(2, 2, 2, 2),           array(3, 3, 3, 3),           array(4, 4, 4, 4)); $N = 4; transpose($A, $B); echo "Result matrix is \n";for ($i = 0; $i < $N; $i++){    for ($j = 0; $j < $N; $j++)    {        echo $B[$i][$j];        echo " ";    }    echo "\n";} // This code is contributed// by Shivi_Aggarwal?>
<script>// javascript Program to find// transpose of a matrix        var N = 4;     // This function stores transpose    // of A in B    function transpose(A , B)    {        var i, j;        for (i = 0; i < N; i++)            for (j = 0; j < N; j++)                B[i][j] = A[j][i];    }     // Driver code         var A = [             [ 1, 1, 1, 1 ],             [ 2, 2, 2, 2 ],             [ 3, 3, 3, 3 ],             [ 4, 4, 4, 4 ] ];     var B =   Array(4).fill(0);    for(let i = 0; i < N; i++){       B[i] = new  Array(N);     }     transpose(A, B);     document.write("Result matrix is <br/>");    for (let i = 0; i < N; i++)    {        for (let j = 0; j < N; j++)            document.write(B[i][j] + " ");        document.write("<br/>");    } // This code is contributed by gauravrajput1 </script>
Output: 
Result matrix is
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
For Rectangular Matrix :
The below program finds transpose of A[][] and stores the result in B[][]. 
C++
C
Java
Python3
C#
PHP
Javascript
// 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 codeint 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;} // This code is contributed by shivanisinghss2110
#include <stdio.h>#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];} 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);     printf("Result matrix is \n");    for (i = 0; i < N; i++)    {        for (j = 0; j < M; j++)        printf("%d ", B[i][j]);        printf("\n");    }     return 0;}
// Java Program to find// transpose of a matrix class GFG{    static final int M = 3;    static final int N = 4;         // This function stores transpose    // of A[][] in B[][]    static void transpose(int A[][], int B[][])    {        int i, j;        for (i = 0; i < N; i++)            for (j = 0; j < M; j++)                B[i][j] = A[j][i];    }         // Driver code    public static void main (String[] args)    {        int A[][] = { {1, 1, 1, 1},                      {2, 2, 2, 2},                      {3, 3, 3, 3}};             int B[][] = new int[N][M], i, j;             transpose(A, B);             System.out.print("Result matrix is \n");        for (i = 0; i < N; i++)        {            for (j = 0; j < M; j++)            System.out.print(B[i][j] + " ");            System.out.print("\n");        }    }} // This code is contributed by Anant Agarwal.
# Python3 Program to find# transpose of a matrix M = 3N = 4 # This function stores# transpose of A[][] in B[][] def transpose(A, B):     for i in range(N):        for j in range(M):            B[i][j] = A[j][i] # driver codeA = [ [1, 1, 1, 1],    [2, 2, 2, 2],    [3, 3, 3, 3]]  # To store resultB = [[0 for x in range(M)] for y in range(N)] transpose(A, B) print("Result matrix is")for i in range(N):    for j in range(M):        print(B[i][j], " ", end='')    print()   
// C# Program to find// transpose of a matrix using System;class GFG {     static  int M = 3;    static  int N = 4;          // This function stores transpose    // of A[][] in B[][]    static void transpose(int [,]A, int [,]B)    {        int i, j;        for (i = 0; i < N; i++)            for (j = 0; j < M; j++)                B[i,j] = A[j,i];    }          // Driver code    public static void Main ()    {        int [,]A = { {1, 1, 1, 1},                      {2, 2, 2, 2},                      {3, 3, 3, 3}};              int [,]B= new int[N,M];              transpose(A, B);              Console.WriteLine("Result matrix is \n");        for (int i = 0; i < N; i++)        {            for (int j = 0; j < M; j++)            Console.Write(B[i,j] + " ");            Console.Write("\n");        }    }}  // This code is contributed by nitin mittal
<?php // This function stores transpose// of A[][] in B[][]function transpose(&$A, &$B){    $N = 4;    $M = 3;    for ($i = 0; $i < $N; $i++)        for ($j = 0; $j < $M; $j++)            $B[$i][$j] = $A[$j][$i];} // Driver code $A = array(array(1, 1, 1, 1),           array(2, 2, 2, 2),           array(3, 3, 3, 3)); $N = 4;$M = 3;transpose($A, $B); echo "Result matrix is \n";for ($i = 0; $i < $N; $i++){    for ($j = 0; $j < $M; $j++)    {    echo $B[$i][$j];    echo " ";    }    echo "\n";} // This code is contributed// by Shivi_Aggarwal?>
<script>// javascript Program to find// transpose of a matrix   var M = 3;     var N = 4;     // This function stores transpose    // of A in B    function transpose(A , B) {        var i, j;        for (i = 0; i < N; i++)            for (j = 0; j < M; j++)                B[i][j] = A[j][i];    }     // Driver code             var A = [ [ 1, 1, 1, 1 ], [ 2, 2, 2, 2 ], [ 3, 3, 3, 3 ]];         var B = Array(N);        for(i=0;i<N;i++)        B[i] =Array(M).fill(0);         transpose(A, B);         document.write("Result matrix is <br/>");        for (i = 0; i < N; i++) {            for (j = 0; j < M; j++)                document.write(B[i][j] + " ");            document.write("<br/>");        }// This code contributed by Rajput-Ji</script>
Output:  
Result matrix is
1 2 3 
1 2 3 
1 2 3 
1 2 3
In-Place for Square Matrix: 
C++
Java
Python3
C#
PHP
Javascript
#include <bits/stdc++.h>using namespace std; #define N 4 // Converts A[][] to its transposevoid 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]);} 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;}
// Java Program to find// transpose of a matrix class GFG{    static final int N = 4;         // Finds transpose of A[][] in-place    static void transpose(int A[][])    {        for (int i = 0; i < N; i++)            for (int j = i+1; j < N; j++)            {                 int temp = A[i][j];                 A[i][j] = A[j][i];                 A[j][i] = temp;            }    }         // Driver code    public static void main (String[] args)    {        int A[][] = { {1, 1, 1, 1},                      {2, 2, 2, 2},                      {3, 3, 3, 3},                      {4, 4, 4, 4}};                transpose(A);             System.out.print("Modified matrix is \n");        for (int i = 0; i < N; i++)        {            for (int j = 0; j < N; j++)            System.out.print(A[i][j] + " ");            System.out.print("\n");        }    }}
# Python3 Program to find# transpose of a matrix N = 4  # Finds transpose of A[][] in-placedef transpose(A):     for i in range(N):        for j in range(i+1, N):            A[i][j], A[j][i] = A[j][i], A[i][j] # driver codeA = [ [1, 1, 1, 1],    [2, 2, 2, 2],    [3, 3, 3, 3],    [4, 4, 4, 4]]   transpose(A)  print("Modified matrix is")for i in range(N):    for j in range(N):        print(A[i][j], " ", end='')    print()     # This code is contributed# by Anant Agarwal.
// C# Program to find transpose of// a matrixusing System; class GFG {         static int N = 4;         // Finds transpose of A[][] in-place    static void transpose(int [,]A)    {        for (int i = 0; i < N; i++)            for (int j = i+1; j < N; j++)            {                int temp = A[i,j];                A[i,j] = A[j,i];                A[j,i] = temp;            }    }         // Driver code    public static void Main ()    {        int [,]A = { {1, 1, 1, 1},                    {2, 2, 2, 2},                    {3, 3, 3, 3},                    {4, 4, 4, 4}};                 transpose(A);             Console.WriteLine("Modified matrix is ");        for (int i = 0; i < N; i++)        {            for (int j = 0; j < N; j++)                Console.Write(A[i,j] + " ");                             Console.WriteLine();        }    }} // This code is contributed by anuj_67.
<?php// Converts A[][] to its transposefunction transpose(&$A){    $N = 4;    for ($i = 0; $i < $N; $i++)        for ($j = $i + 1; $j < $N; $j++)            {                $temp = $A[$i][$j];                $A[$i][$j] = $A[$j][$i];                $A[$j][$i] = $temp;            }} // Driver Code$N = 4;$A = array(array(1, 1, 1, 1),            array(2, 2, 2, 2),           array(3, 3, 3, 3),           array(4, 4, 4, 4)); transpose($A); echo "Modified matrix is " . "\n";for ($i = 0; $i < $N; $i++){    for ($j = 0; $j < $N; $j++)        echo $A[$i][$j] . " ";    echo "\n";} // This code is contributed// by Akanksha Rai(Abby_akku)?>
<script> // JavaScript Program to find// transpose of a matrix   var N = 4;     // Finds transpose of A in-place    function transpose(A) {        for (i = 0; i < N; i++)            for (j = i + 1; j < N; j++) {                var temp = A[i][j];                A[i][j] = A[j][i];                A[j][i] = temp;            }    }     // Driver code             var A = [ [ 1, 1, 1, 1 ],                  [ 2, 2, 2, 2 ],                  [ 3, 3, 3, 3 ],                  [ 4, 4, 4, 4 ] ];         transpose(A);         document.write("Modified matrix is <br/>");        for (i = 0; i < N; i++) {            for (j = 0; j < N; j++)                document.write(A[i][j] + " ");            document.write("\<br/>");        } // This code is contributed by aashish1995 </script>
Output: 
Modified matrix is
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Note- 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 transpose of a matrix using identity matrix as reference matrix is O(m*n). Suppose, if the given matrix is a square matrix, the running time will be O(n2).
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
AbhijeetPrakash
nitin mittal
vt_m
Shivi_Aggarwal
Akanksha_Rai
Himanshu Srivastav 2
GauravRajput1
Rajput-Ji
aashish1995
shivanisinghss2110
gargr0109
Amazon
MakeMyTrip
Wipro
Matrix
School Programming
Amazon
MakeMyTrip
Wipro
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum size square sub-matrix with all 1s
Sudoku | Backtracking-7
Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
Maximum size rectangle binary sub-matrix with all 1s
Program to multiply two matrices
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects