Q:

Program to find the transpose of a given matrix

belongs to collection: Matrix Programs

0

Explanation

In this program, we need to find the transpose of the given matrix and print the resulting matrix.

Transpose of a matrix:

Transpose of a matrix can be found by interchanging rows with the column that is, rows of the original matrix will become columns of the new matrix. Similarly, columns in the original matrix will become rows in the new matrix. The operation can be represented as follows:

[ AT ]ij = [ A ]ji

If the dimension of the original matrix is 2 × 3 then, the dimensions of the new transposed matrix will be 3 × 2.

Algorithm

  1. Declare and initialize a two-dimensional array a.
  2. Calculate the number of rows and columns present in the matrix and store it variables rows and cols respectively.
  3. Declare another array t with reversed dimensions i.e t[cols][rows]. Array t will be used to store the elements of the transposed matrix.
  4. Loop through the array a and convert its rows into columns of matrix t using
    t[ i ][ j ] = a[ j ][ i ];
  5. Finally, display the elements of matrix t.

Input:

Matrix a = [1, 2, 3]  

           [4, 5, 6]  

           [7, 8, 9]  

Output:

Transpose of given matrix: [1 4 7]
                           [2 5 8]
                           [3 6 9]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

#Initialize matrix a  
a = [  
        [1, 2, 3],  
        [4, 5, 6],  
        [7, 8, 9]  
    ];  
   
#Calculates number of rows and columns present in given matrix  
rows = len(a);  
cols = len(a[0]);  
   
#Declare array t with reverse dimensions and is initialized with zeroes.  
t = [[0]*rows for i in range(cols)];  
   
#Calculates transpose of given matrix  
for i in range(0, cols):  
    for j in range(0, rows):  
        #Converts the row of original matrix into column of transposed matrix  
        t[i][j] = a[j][i];  
   
print("Transpose of given matrix: ");  
for i in range(0, cols):  
    for j in range(0, rows):  
        print(t[i][j]),  
   
    print(" ");  

 

Output:

Transpose of given matrix: 
1 4 7 
2 5 8 
3 6 9 

 

C

#include <stdio.h>  
   
int main()  
{  
    int rows, cols;  
          
    //Initialize matrix a  
    int a[][3] = {  
                    {1, 2, 3},  
                    {4, 5, 6},  
                    {7, 8, 9}  
                };  
      
    //Calculates number of rows and columns present in given matrix  
    rows = (sizeof(a)/sizeof(a[0]));  
    cols = (sizeof(a)/sizeof(a[0][0]))/rows;  
      
    //Declare array t with reverse dimensions  
    int t[cols][rows];  
      
    //Calculates transpose of given matrix  
    for(int i = 0; i < cols; i++){  
        for(int j = 0; j < rows; j++){  
            //Converts the row of original matrix into column of transposed matrix  
            t[i][j] = a[j][i];  
        }  
    }  
      
    printf("Transpose of given matrix: \n");  
    for(int i = 0; i < cols; i++){  
        for(int j = 0; j < rows; j++){  
           printf("%d ", t[i][j]);  
        }  
        printf("\n");  
    }  
   
    return 0;  
}  

 

Output:

Transpose of given matrix: 
1 4 7 
2 5 8 
3 6 9 

 

JAVA

public class Transpose  
{  
    public static void main(String[] args) {  
        int rows, cols;  
          
        //Initialize matrix a  
          int a[][] = {  
                          {1, 2, 3},  
                          {4, 5, 6},  
                          {7, 8, 9}  
                       };  
            
          //Calculates number of rows and columns present in given matrix  
          rows = a.length;  
        cols = a[0].length;  
          
        //Declare array t with reverse dimensions  
        int t[][] = new int[cols][rows];  
          
        //Calculates transpose of given matrix  
        for(int i = 0; i < cols; i++){  
            for(int j = 0; j < rows; j++){  
                //Converts the row of original matrix into column of transposed matrix  
                t[i][j] = a[j][i];  
            }  
        }  
          
        System.out.println("Transpose of given matrix: ");  
        for(int i = 0; i < cols; i++){  
            for(int j = 0; j < rows; j++){  
               System.out.print(t[i][j] + " ");  
            }  
            System.out.println();  
        }  
    }  
}  

 

Output:

Transpose of given matrix: 
1 4 7 
2 5 8 
3 6 9 

 

C#

using System;  
                      
public class Transpose  
{  
    public static void Main()  
    {  
        int rows, cols;  
          
        //Initialize matrix a  
          int[,] a = {  
                          {1, 2, 3},  
                          {4, 5, 6},  
                          {7, 8, 9}  
                     };  
            
          //Calculates number of rows and columns present in given matrix  
          rows = a.GetLength(0);  
        cols = a.GetLength(1);  
          
        //Declare array t with reverse dimensions  
        int[,] t = new int[cols,rows];  
          
        //Calculates transpose of given matrix  
        for(int i = 0; i < cols; i++){  
            for(int j = 0; j < rows; j++){  
                //Converts the row of original matrix into column of transposed matrix  
                t[i,j] = a[j,i];  
            }  
        }  
          
        Console.WriteLine("Transpose of given matrix: ");  
        for(int i = 0; i < cols; i++){  
            for(int j = 0; j < rows; j++){  
               Console.Write(t[i,j] + " ");  
            }  
            Console.WriteLine();  
        }  
    }  
}  

 

Output:

Transpose of given matrix: 
1 4 7 
2 5 8 
3 6 9 

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
//Initialize matrix a  
$a = array(  
            array(1, 2, 3),  
            array(4, 5, 6),  
            array(7, 8, 9)  
           );  
   
//Calculates number of rows and columns present in given matrix  
$rows = count($a);  
$cols = count($a[0]);  
   
//Declare array t with reverse dimensions and initialize it with 0  
$t = array_fill(0, $cols, array_fill(0, $rows, 0));  
   
//Calculates transpose of given matrix  
for($i = 0; $i < $cols; $i++){  
    for($j = 0; $j < $rows; $j++){  
        //Converts the row of original matrix into column of transposed matrix  
        $t[$i][$j] = $a[$j][$i];  
    }  
}  
   
print("Transpose of given matrix: <br>");  
for($i = 0; $i < $cols; $i++){  
    for($j = 0; $j < $rows; $j++){  
       print($t[$i][$j] . " ");  
    }  
    print("<br>");  
}  
?>  
</body>  
</html>  

 

Output:

Transpose of given matrix: 
1 4 7 
2 5 8 
3 6 9 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Program to find the sum of each row and each colum...