Question:
Write a program to transpose two matrices
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#! usr/bin/python m=int(input("ENTER MARTIX ROW SIZE m : ")) n=int(input("ENTER MARTIX COLUMN SIZE n : ")) #initializing matrix elements as 0 X = [[0]*n for j in range(m)] Y = [[0]*m for j in range(n)] #getting input to matrix X for i in range (m): for j in range (n): print ('entry in row: ',i+1,' column: ',j+1) X[i][j] = int(input()) #printing first matrix X print "ORIGINAL MATRIX : " for i in range (m): for j in range (n): print X[i][j],"\t", print "\n" #transpose of matrix X for i in range (m): for j in range (n): Y[j][i]=X[i][j] #printing transpose matrix Y print "TRANSPOSE MATRIX : " for i in range (n): for j in range (m): print Y[i][j],"\t", print "\n" |
Explanation:
The transpose of a matrix is an operator which flips a matrix over its diagonal, that is it switches the row and column indices of the matrix by producing another matrix denoted as AT (also written A′, Atr, tA or At). It is achieved by any one of the following equivalent actions:
Formally, the i th row, j th column element of AT is the j th row, i th column element of A:
If A is an m × n matrix then AT is an n × m matrix.
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
ENTER MARTIX ROW SIZE m : 2 ENTER MARTIX COLUMN SIZE n : 2 entry in row: 1 column: 1 8 entry in row: 1 column: 2 9 entry in row: 2 column: 1 4 entry in row: 2 column: 2 8 ORIGINAL MATRIX : 8 4 9 8 TRANSPOSE MATRIX : 8 9 4 8 |
Leave a Reply