C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to find the transpose of a given matrixExplanationIn 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
SolutionPython#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 JAVApublic 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#
|