Tutorialspoint.dev

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].

matrix-transpose


For Square Matrix :

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

C++

#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 ");
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
           printf("%d ", B[i][j]);
        printf(" ");
    }
  
    return 0;
}

/div>

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;
      
        transpose(A, B);
      
        System.out.print("Result matrix is ");
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < N; j++)
            System.out.print(B[i][j] + " ");
            System.out.print(" ");
        }
    }
}
  
// 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
A = [ [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3],
    [4, 4, 4, 4]]
   
   
B = A[:][:] # 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#

// 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 calling
        transpose(A, B);
      
        Console.Write("Result matrix is ");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            Console.Write(B[i,j] + " ");
            Console.Write(" ");
        }
    }
}
  
// This code is contributed by nitin mittal.

PHP

<?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 ";
for ($i = 0; $i < $N; $i++)
{
    for ($j = 0; $j < $N; $j++)
    {
        echo $B[$i][$j];
        echo " ";
    }
    echo " ";
}
  
// This code is contributed
// by Shivi_Aggarwal 
?>

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

#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 ");
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < M; j++)
        printf("%d ", B[i][j]);
        printf(" ");
    }
  
    return 0;
}

Java

// 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 ");
        for (i = 0; i < N; i++)
        {
            for (j = 0; j < M; j++)
            System.out.print(B[i][j] + " ");
            System.out.print(" ");
        }
    }
}
  
// This code is contributed by Anant Agarwal.

Python3

# Python3 Program to find
# transpose of a matrix
  
M = 3
N = 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 code
A = [ [1, 1, 1, 1],
    [2, 2, 2, 2],
    [3, 3, 3, 3]]
  
  
# To store result
B = [[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#

// 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 ");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            Console.Write(B[i,j] + " ");
            Console.Write(" ");
        }
    }
}
   
// This code is contributed by nitin mittal

PHP

<?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 ";
for ($i = 0; $i < $N; $i++)
{
    for ($j = 0; $j < $M; $j++)
    {
    echo $B[$i][$j];
    echo " ";
    }
    echo " ";
}
  
// This code is contributed
// by Shivi_Aggarwal 
?>


Output:

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

 

In-Place for Square Matrix:

C++

#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]);
}
  
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 ");
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
           printf("%d ", A[i][j]);
        printf(" ");
    }
  
    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 ");
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            System.out.print(A[i][j] + " ");
            System.out.print(" ");
        }
    }
}

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.

PHP

<?php
// Converts A[][] to its transpose
function 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 " . " ";
for ($i = 0; $i < $N; $i++)
{
    for ($j = 0; $j < $N; $j++)
        echo $A[$i][$j] . " ";
    echo " ";
}
  
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>


Output:

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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter