TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Find The Transpose of a Given Matrix

Program to Find The Transpose of a Given Matrix on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Program to find the transpose of a given matrix

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
Program to find the transpose of a given matrix

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.

Solution

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 

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf