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:
Code Snippet:
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:
- using System;
- public class TransMat
- {
- public static void Main()
- {
- int m, n, row, col;
- int[,] matA = new int[10,10];
- Console.Write("\nEnter the no. of row: ");
- m = Convert.ToInt16(Console.ReadLine());
- Console.Write("Enter the no. of col: ");
- n = Convert.ToInt16(Console.ReadLine());
- Console.WriteLine("\nEnter the element in the matrix: ");
- for(row=1; row<=m; row++)
- {
- for(col = 1; col<=n; col++)
- {
- Console.Write("Matrix A[{0},{1}]: ", row, col );
- matA[row,col] = Convert.ToInt16(Console.ReadLine());
- }
- Console.WriteLine();
- }
- Console.WriteLine("Matrix A :");
- for(row=1; row<=m; row++)
- {
- for(col = 1; col<=n; col++)
- Console.Write("{0:D2} ", matA[row,col]);
- Console.WriteLine();
- }
- Console.ReadLine();
- Console.WriteLine("Matrix A\':");
- for(row=1; row<=n; row++)
- {
- for(col = 1; col<=m; col++)
- Console.Write("{0:D2} ", matA[col,row]);
- Console.WriteLine();
- }
- Console.ReadLine();
- }
- }
** Complete code
Description:
To transpose the matrix we simply change the rows into columns and columns into row.
- for(row=1; row<=n; row++)
- {
- for(col = 1; col<=m; col++)
- Console.Write("{0:D2} ", matA[col,row]);
- Console.WriteLine();
- }
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: