Tuesday 31 March 2015

Transpose of Matrix

Question: Program to transpose a matrix.

Definition: If A matrix be an m x n matrix, then the matrix obtained by interchanging the rows and columns of A is called the transpose of matrix A. Transpose of the matrix A is denoted by A'. In other words, if A is the order of m x n. then A' is the order of n x m.


Properties of transpose of matrix:

  • (A')' = A
  • (k A)' = kA'
  • (A + B)' = A' +B'
  • (AB)' = B' A'


Code Snippet:

  1. using System;  
  2. public class TransMat  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int m, n, row, col;  
  7.         int[,] matA = new int[10,10];  
  8.   
  9.         Console.Write("\nEnter the no. of row: ");  
  10.         m = Convert.ToInt16(Console.ReadLine());  
  11.   
  12.         Console.Write("Enter the no. of col: ");  
  13.         n = Convert.ToInt16(Console.ReadLine());  
  14.   
  15.         Console.WriteLine("\nEnter the element in the matrix: ");  
  16.         for(row=1; row<=m; row++)  
  17.         {  
  18.             for(col = 1; col<=n; col++)  
  19.             {  
  20.                 Console.Write("Matrix A[{0},{1}]: ", row, col );  
  21.                 matA[row,col] = Convert.ToInt16(Console.ReadLine());  
  22.             }  
  23.   
  24.             Console.WriteLine();  
  25.         }  
  26.   
  27.         Console.WriteLine("Matrix A :");  
  28.         for(row=1; row<=m; row++)  
  29.         {  
  30.             for(col = 1; col<=n; col++)  
  31.             Console.Write("{0:D2} ", matA[row,col]);  
  32.   
  33.             Console.WriteLine();  
  34.         }  
  35.   
  36.             Console.ReadLine();  
  37.   
  38.         Console.WriteLine("Matrix A\':");  
  39.         for(row=1; row<=n; row++)  
  40.         {  
  41.             for(col = 1; col<=m; col++)  
  42.             Console.Write("{0:D2} ", matA[col,row]);  
  43.   
  44.             Console.WriteLine();  
  45.         }  
  46.   
  47.         Console.ReadLine();  
  48.           
  49.     }  
  50. }
** Complete code

Description:

To  transpose the matrix we simply change the rows into columns and columns into row.

  1. for(row=1; row<=n; row++)  
  2.         {  
  3.             for(col = 1; col<=m; col++)  
  4.             Console.Write("{0:D2} ", matA[col,row]);  
  5.   
  6.             Console.WriteLine();  
  7.         } 

In the above code n is the number of columns which refers the row now and m is the number of row which refers the columns now.


Screenshot of Output:

Output of Transpose