Open In App
Related Articles

# Program to find transpose of a matrix

Given a matrix of size N X M, find the transpose of the matrix
Transpose 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:

Approach: Follow the given steps to solve the problem:

• Run a nested loop using two integer pointers i and j for 0 <= i < N and 0 <= j < M
• Set B[i][j] equal to A[j][i]

Below is the implementation of the above approach:

## C++

 `// C++ program to find``// transpose of a matrix``#include ``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 } };` `    ``// Note dimensions of B[][]``    ``int` `B[N][N], i, j;` `      ``// Function call``    ``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

 `// C program to find``// transpose of a matrix` `#include ``#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 } };` `    ``// Note dimensions of B[][]``    ``int` `B[N][N], i, j;` `      ``// Function call``    ``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

 `// 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;` `          ``// Function call``        ``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

 `# 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 code``if` `__name__ ``=``=` `'__main__'``:``  ``A ``=` `[[``1``, ``1``, ``1``, ``1``],``       ``[``2``, ``2``, ``2``, ``2``],``       ``[``3``, ``3``, ``3``, ``3``],``       ``[``4``, ``4``, ``4``, ``4``]]`  `  ``# To store result``  ``B ``=` `[[``0` `for` `x ``in` `range``(N)] ``for` `y ``in` `range``(N)]` `  ``# Function call``  ``transpose(A, B)` `  ``print``(``"Result matrix is"``)``  ``for` `i ``in` `range``(N):``      ``for` `j ``in` `range``(N):``          ``print``(B[i][j], ``" "``, end``=``'')``      ``print``()`

## C#

 `// C# Program to find``// transpose of a matrix``using` `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 call``        ``transpose(A, B);` `        ``Console.WriteLine(``"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`

## Javascript

 ``

## PHP

 ``

Output

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

Time complexity: O(M x N).
Auxiliary Space: O(M x N), since M x N extra space has been used.

## Program to find the transpose of a matrix using constant space:

NoteThis approach works only for square matrices (i.e., – where no. of rows are equal to the number of columns). This algorithm is also known as an “in-place” algorithm as it uses no extra space to solve the problem.

Follow the given steps to solve the problem:

• Run a nested loop using two integer pointers i and j for 0 <= i < N and i+1 <= j < N
• Swap A[i][j] with A[j][i]

Below is the implementation of the above approach:

## C++

 `#include ``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]);``}` `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

 `// 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

 `# Python3 Program to find``# transpose of a matrix` `N ``=` `4` `# Finds transpose of A[][] in-place`  `def` `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 code``A ``=` `[[``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#

 `// C# Program to find transpose of``// a matrix``using` `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.`

## Javascript

 ``

## PHP

 ``

Output

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

Time complexity: O(N2).
Auxiliary Space: O(1)