{"id":7132,"date":"2021-08-16T12:23:51","date_gmt":"2021-08-16T06:53:51","guid":{"rendered":"https:\/\/python-programs.com\/?p=7132"},"modified":"2021-11-22T18:39:25","modified_gmt":"2021-11-22T13:09:25","slug":"program-for-transpose-a-matrix-in-python-cpp-programming","status":"publish","type":"post","link":"https:\/\/python-programs.com\/program-for-transpose-a-matrix-in-python-cpp-programming\/","title":{"rendered":"Program for Transpose a Matrix in Python & C++ Programming"},"content":{"rendered":"
In the previous article, we have discussed about\u00a0Program for addition of two matrices in Python & C++ Programming<\/a>. Let us learn Program for Transpose a Matrix in C++ Program and Python.<\/p>\n What is a matrix:<\/strong><\/p>\n A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.<\/p>\n Example:<\/strong><\/p>\n <\/p>\n Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.<\/p>\n In this order, the dimensions of a matrix indicate the number of rows and columns.<\/p>\n Here as there are 5 rows and 4 columns it is called as 5*4 matrix.<\/p>\n What is Matrix Transpose:<\/strong><\/p>\n The interchanging of rows and columns is known as a matrix transpose. It’s abbreviated as A’. The element in A’s ith row and jth column will be moved to A’s jth row and ith column.<\/p>\n <\/p>\n Examples for matrix Transpose:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n A matrix can be implemented as a nested list in Python (list inside a list). Each variable can be thought of as a row in the matrix.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The program\u2019s output is the same as previously. For iterating through each element in the array, we used the nested list comprehension.<\/p>\n Understanding the list helps us to write concise codes and we need to always try to use them in Python. They\u2019re really beneficial.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We used nesting loops in this program to iterate through and row and column.<\/p>\n Calculate the transpose matrix by initializing transpose[i][j]=A[j][i].In the\u00a0 the appropriate elements and store them in the result at each level.<\/p>\n Let us take dynamic input in this case.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n In the previous article, we have discussed about\u00a0Program for addition of two matrices in Python & C++ Programming. Let us learn Program for Transpose a Matrix in C++ Program and Python. What is a matrix: A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a …<\/p>\nMatrix 1 = 2 3 1 \r\n 1 2 3<\/pre>\n
Printing the transpose of matrices : \r\n2 1\r\n3 2\r\n1 3<\/pre>\n
Program for Matrix Transpose<\/h2>\n
\n
Method #1:Using nested Loops in Python<\/h3>\n
\n
# given matrix A\r\nA = [[2, 3, 1],\r\n [1, 2, 3]]\r\nrows = len(A)\r\ncolumns = len(A[0])\r\n# Initialize the transpose of matrices elements to 0\r\n# with rows as columns and columns as rows as dimensions\r\nmatrixTrans = [[0, 0],\r\n [0, 0],\r\n [0, 0]]\r\n# iterate through rows\r\nfor i in range(rows):\r\n # iterate through columns\r\n for j in range(columns):\r\n matrixTrans[j][i] = A[i][j]\r\n# printing the transpose of matrices\r\nprint(\"Printing the transpose of matrices : \")\r\nfor rows in matrixTrans:\r\n print(*rows)\r\n<\/pre>\n
Printing the transpose of matrices : \r\n2 1\r\n3 2\r\n1 3<\/pre>\n
Method #2:Using List Comprehension in Python<\/h3>\n
# given matrix A\r\nA = [[2, 3, 1],\r\n [1, 2, 3]]\r\nrows = len(A)\r\ncolumns = len(A[0])\r\n# Initialize the transpose of matrices elements to 0\r\n# with rows as columns and columns as rows as dimensions\r\nmatrixTrans = [[0, 0],\r\n [0, 0],\r\n [0, 0]]\r\n# using list comprehension to transpose a matrix\r\nmatrixTrans = [[A[j][i] for j in range(rows)] for i in range(columns)]\r\n# printing the transpose of matrices\r\nprint(\"Printing the transpose of matrices : \")\r\nfor rows in matrixTrans:\r\n print(*rows)\r\n<\/pre>\n
Printing the transpose of matrices : \r\n2 1\r\n3 2\r\n1 3<\/pre>\n
Method #3:Using nested loops in C++<\/h3>\n
#include <iostream>\r\nusing namespace std;\r\nint main()\r\n{\r\n int rows, columns, A[100][100], matrixTrans[100][100], i, j;\r\n cout << \"Enter the number of rows of the matrix \"<<endl;\r\n cin >> rows;\r\n cout << \"Enter the number of columns of the matrix\"<<endl;\r\n cin >> columns;\r\n cout << \"Enter the elements of the matrix \" << endl;\r\n \/\/ Initializing matrix A with the user defined values\r\n for(i = 0; i < rows; ++i)\r\n for(j = 0; j < columns; ++j)\r\n {\r\n cout << \"Enter element A\" << i + 1 << j + 1 << \" = \";\r\n cin >> A[i][j];\r\n }\r\n \r\n \/\/ Calculating transpose of the matrix\r\n for(i = 0; i < rows; ++i)\r\n for(j = 0; j < columns; ++j)\r\n matrixTrans[j][i] = A[i][j];\r\n \/\/printing matrix A\r\n cout << endl << \" printing the matrix A\" << endl;\r\n for (i = 0; i < rows; ++i) {\r\n for (j = 0; j < columns; ++j) {\r\n cout << A[i][j] << \" \";\r\n }\r\n cout << endl;\r\n}\r\n \r\n \/\/printing the transpose of matrices\r\n cout << endl << \" printing the transpose of matrices A \" << endl;\r\n \r\n for (i = 0; i < columns; ++i) {\r\n for (j = 0; j < rows; ++j) {\r\n cout << matrixTrans[i][j] << \" \";\r\n }\r\n cout << endl;\r\n}\r\n return 0;\r\n}<\/pre>\n
Enter the number of rows of the matrix \r\n5\r\nEnter the number of columns of the matrix\r\n4\r\nEnter the elements of the matrix \r\nEnter element A11 = 1\r\nEnter element A12 = 2\r\nEnter element A13 = 3\r\nEnter element A14 = 4\r\nEnter element A21 = 5\r\nEnter element A22 = 8\r\nEnter element A23 = 9\r\nEnter element A24 = 7\r\nEnter element A31 = 2\r\nEnter element A32 = 1\r\nEnter element A33 = 6\r\nEnter element A34 = 4\r\nEnter element A41 = 5\r\nEnter element A42 = 2\r\nEnter element A43 = 8\r\nEnter element A44 = 0\r\nEnter element A51 = -5\r\nEnter element A52 = 3\r\nEnter element A53 = 4\r\nEnter element A54 = 8\r\n\r\nprinting the matrix A\r\n1 2 3 4 \r\n5 8 9 7 \r\n2 1 6 4 \r\n5 2 8 0 \r\n-5 3 4 8\r\n\r\nprinting the transpose of matrices A \r\n1 5 2 5 -5 \r\n2 8 1 2 3 \r\n3 9 6 8 4 \r\n4 7 4 0 8<\/pre>\n
\n