{"id":3078,"date":"2020-02-04T12:14:36","date_gmt":"2020-02-04T12:14:36","guid":{"rendered":"https:\/\/www.askpython.com\/?p=3078"},"modified":"2023-02-16T19:57:17","modified_gmt":"2023-02-16T19:57:17","slug":"python-matrix-tutorial","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-matrix-tutorial","title":{"rendered":"Python Matrix Tutorial"},"content":{"rendered":"\n<p>We can implement a Python Matrix in the form of a <strong>2-d List<\/strong> or a <strong>2-d Array<\/strong>. <strong>To perform operations on Python Matrix, we need to import Python NumPy Module.<\/strong><\/p>\n\n\n\n<p>Python Matrix is essential in the field of statistics, data processing, image processing, etc.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Creation of a Python Matrix<\/h2>\n\n\n\n<p>Python Matrix can be created using one of the following techniques:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>By using Lists<\/strong><\/li><li><strong>By using arange() method<\/strong><\/li><li><strong>By using matrix() method<\/strong><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1. Creation of matrix using Lists<\/h3>\n\n\n\n<p>The <code>numpy.array()<\/code> function can be used to create an array using <strong>lists as input to it<\/strong>.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\ninput_arr = numpy.array(&#x5B;&#x5B; 10, 20, 30],&#x5B; 40, 50, 60]])\nprint(input_arr)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;10 20 30]\n &#x5B;40 50 60]]\n<\/pre><\/div>\n\n\n<p>As seen above, the output represents a 2-D matrix with the given set of inputs in the form of list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Creation of matrix using &#8216;numpy.arange()&#8217; function<\/h3>\n\n\n\n<p>The <code>numpy.arange()<\/code> function along with the list inputs can be used to create a matrix in Python.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\n\nprint(numpy.array(&#x5B;numpy.arange(10,15), numpy.arange(15,20)]))\n \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;10 11 12 13 14]\n &#x5B;15 16 17 18 19]]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. Creation of Matrix using &#8216;numpy.matrix() function&#8217;<\/h3>\n\n\n\n<p>The <code>numpy.matrix()<\/code> function enables us to create a matrix in Python.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.matrix(input,dtype)\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li><strong>input: The elements input to form a matrix.<\/strong><\/li><li><strong>dtype: The data type of the corresponding output.<\/strong><\/li><\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as p\n\n\nmatA = p.matrix(&#x5B;&#x5B;10, 20], &#x5B;30, 40]])  \nprint(&#039;MatrixA:\\n&#039;, matA)\n\n\n\nmatB = p.matrix(&#039;&#x5B;10,20;30,40]&#039;, dtype=p.int32)  # Setting the data-type to int\nprint(&#039;\\nMatrixB:\\n&#039;, matB)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMatrixA:\n &#x5B;&#x5B;10 20]\n &#x5B;30 40]]\n\nMatrixB:\n &#x5B;&#x5B;10 20]\n &#x5B;30 40]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Addition of Matrix in Python<\/h2>\n\n\n\n<p>The addition operation on Matrices can be performed in the following ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Traditional method<\/strong><\/li><li><strong>By using &#8216;+&#8217; operator<\/strong><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1. Traditional method<\/h3>\n\n\n\n<p>In this traditional method, we basically take the input from the user and then perform the addition operation using the <strong>for loops<\/strong> (to traverse through the elements of the matrix) and <strong>&#8216;+&#8217; operator<\/strong>. <\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as p\n\n\nar1 = p.matrix(&#x5B;&#x5B;11, 22], &#x5B;33, 44]])  \nar2 = p.matrix(&#x5B;&#x5B;55, 66], &#x5B;77, 88]])  \nres = p.matrix(p.zeros((2,2)))  \nprint(&#039;Matrix ar1 :\\n&#039;, ar1)\nprint(&#039;\\nMatrix ar2 :\\n&#039;, ar2)\n\n# traditional code\nfor x in range(ar1.shape&#x5B;1]):\n    for y in range(ar2.shape&#x5B;0]):\n        res&#x5B;x, y] = ar1&#x5B;x, y] + ar2&#x5B;x, y]\n\nprint(&#039;\\nResult :\\n&#039;, res)\n\n\n<\/pre><\/div>\n\n\n<p><strong>Note<\/strong>:<code>Matrix.shape<\/code> returns the dimensions of a particular matrix.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMatrix ar1 :\n &#x5B;&#x5B;11 22]\n &#x5B;33 44]]\n\nMatrix ar2 :\n &#x5B;&#x5B;55 66]\n &#x5B;77 88]]\n\nResult :\n &#x5B;&#x5B;  66.   88.]\n &#x5B; 110.  132.]]\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Using &#8216;+&#8217; operator<\/h3>\n\n\n\n<p>This method provides better efficiency to the code as it reduces the LOC (lines of code) and thus, optimizes the code.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as p\n\n\nar1 = p.matrix(&#x5B;&#x5B;11, 22], &#x5B;33, 44]])  \nar2 = p.matrix(&#x5B;&#x5B;55, 66], &#x5B;77, 88]])  \nres = p.matrix(p.zeros((2,2)))  \nprint(&#039;Matrix ar1 :\\n&#039;, ar1)\nprint(&#039;\\nMatrix ar2 :\\n&#039;, ar2)\n\nres = ar1 + ar2 # using &#039;+&#039; operator\n\nprint(&#039;\\nResult :\\n&#039;, res)\n\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMatrix ar1 :\n &#x5B;&#x5B;11 22]\n &#x5B;33 44]]\n\nMatrix ar2 :\n &#x5B;&#x5B;55 66]\n &#x5B;77 88]]\n\nResult :\n &#x5B;&#x5B; 66  88]\n &#x5B;110 132]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Matrix Multiplication in Python<\/h2>\n\n\n\n<p>Matrix Multiplication in Python can be provided using the following ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Scalar Product<\/strong><\/li><li><strong>Matrix Product<\/strong><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Scalar Product<\/h3>\n\n\n\n<p>In the scalar product, <strong>a scalar\/constant value<\/strong> is multiplied by each element of the matrix.<\/p>\n\n\n\n<p>The<strong> &#8216;*&#8217; operator <\/strong>is used to multiply the scalar value with the input matrix elements.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as p\n\nmatA = p.matrix(&#x5B;&#x5B;11, 22], &#x5B;33, 44]])  \n\nprint(&quot;Matrix A:\\n&quot;, matA)\nprint(&quot;Scalar Product of Matrix A:\\n&quot;, matA * 10)\n\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMatrix A:\n &#x5B;&#x5B;11 22]\n &#x5B;33 44]]\nScalar Product of Matrix A:\n &#x5B;&#x5B;110 220]\n &#x5B;330 440]]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Matrix Product<\/h3>\n\n\n\n<p>As mentioned above, we can use <strong>the &#8216;*&#8217; operator only for Scalar multiplication<\/strong>. In order to go ahead with Matrix multiplication, we need to make use of the <code>numpy.dot()<\/code> function.<\/p>\n\n\n\n<p>The <code>numpy.dot()<\/code> function takes <strong>NumPy arrays as parameter<\/strong> values and performs multiplication according to the basic rules of Matrix Multiplication.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as p\n\nmatA = p.matrix(&#x5B;&#x5B;11, 22], &#x5B;33, 44]])  \nmatB = p.matrix(&#x5B;&#x5B;2,2], &#x5B;2,2]])\n\nprint(&quot;Matrix A:\\n&quot;, matA)\nprint(&quot;Matrix B:\\n&quot;, matB)\nprint(&quot;Dot Product of Matrix A and Matrix B:\\n&quot;, p.dot(matA, matB))\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMatrix A:\n &#x5B;&#x5B;11 22]\n &#x5B;33 44]]\nMatrix B:\n &#x5B;&#x5B;2 2]\n &#x5B;2 2]]\nDot Product of Matrix A and Matrix B:\n &#x5B;&#x5B; 66  66]\n &#x5B;154 154]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Subtraction of Python Matrix<\/h2>\n\n\n\n<p>The <strong>&#8216;-&#8216; operator<\/strong> is used to perform Subtraction on Python Matrix.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as p\n\nmatA = p.matrix(&#x5B;&#x5B;11, 22], &#x5B;33, 44]])  \nmatB = p.matrix(&#x5B;&#x5B;2,2], &#x5B;2,2]])\n\nprint(&quot;Matrix A:\\n&quot;, matA)\nprint(&quot;Matrix B:\\n&quot;, matB)\nprint(&quot;Subtraction of Matrix A and Matrix B:\\n&quot;,(matA - matB))\n \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMatrix A:\n &#x5B;&#x5B;11 22]\n &#x5B;33 44]]\nMatrix B:\n &#x5B;&#x5B;2 2]\n &#x5B;2 2]]\nSubtraction of Matrix A and Matrix B:\n &#x5B;&#x5B; 9 20]\n &#x5B;31 42]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Division of Python Matrix<\/h2>\n\n\n\n<p><strong>Scalar Division<\/strong> can be performed on the elements of the Matrix in Python using the <strong>&#8216;\/&#8217; operator<\/strong>.<\/p>\n\n\n\n<p>The &#8216;\/&#8217; operator divides each element of the Matrix with a scalar\/constant value.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as p\n\n\nmatB = p.matrix(&#x5B;&#x5B;2,2], &#x5B;2,2]])\n\n\nprint(&quot;Matrix B:\\n&quot;, matB)\nprint(&quot;Matrix B after Scalar Division operation:\\n&quot;,(matB\/2))\n \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMatrix B:\n &#x5B;&#x5B;2 2]\n &#x5B;2 2]]\nMatrix B after Scalar Division operation:\n &#x5B;&#x5B; 1.  1.]\n &#x5B; 1.  1.]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Transpose of a Python Matrix<\/h2>\n\n\n\n<p>Transpose of a matrix basically involves the <strong>flipping of matrix over the corresponding diagonals<\/strong> i.e. it exchanges the rows and the columns of the input matrix. The rows become the columns and vice-versa.<\/p>\n\n\n\n<p>For example: Let&#8217;s consider a matrix A with dimensions 3&#215;2 i.e 3 rows and 2 columns. After performing transpose operation, the dimensions of the matrix A would be 2&#215;3 i.e 2 rows and 3 columns.<\/p>\n\n\n\n<p><code>Matrix.T<\/code> basically performs the transpose of the input matrix and produces a <strong>new matrix<\/strong> as a result of the transpose operation.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\n \nmatA = numpy.array(&#x5B;numpy.arange(10,15), numpy.arange(15,20)])\nprint(&quot;Original Matrix A:\\n&quot;)\nprint(matA)\nprint(&#039;\\nDimensions of the original MatrixA: &#039;,matA.shape)\nprint(&quot;\\nTranspose of Matrix A:\\n &quot;)\nres = matA.T\nprint(res)\nprint(&#039;\\nDimensions of the Matrix A after performing the Transpose Operation:  &#039;,res.shape)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nOriginal Matrix A:\n\n&#x5B;&#x5B;10 11 12 13 14]\n &#x5B;15 16 17 18 19]]\n\nDimensions of the original MatrixA: (2, 5)\n\nTranspose of Matrix A:\n \n&#x5B;&#x5B;10 15]\n &#x5B;11 16]\n &#x5B;12 17]\n &#x5B;13 18]\n &#x5B;14 19]]\n\nDimensions of the Matrix A after performing the Transpose Operation: (5, 2)\n<\/pre><\/div>\n\n\n<p>In the above snippet of code, I have created a matrix of dimensions 2&#215;5 i.e. 2 rows and 5 columns.<\/p>\n\n\n\n<p>After performing the transpose operation, the dimensions of the resultant matrix are 5&#215;2 i.e. 5 rows and 2 columns.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Exponent of a Python Matrix<\/h2>\n\n\n\n<p>The exponent on a Matrix is calculated <strong>element-wise<\/strong> i.e. exponent of every element is calculated by raising the element to the power of an input scalar\/constant value.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\n \nmatA = numpy.array(&#x5B;numpy.arange(0,2), numpy.arange(2,4)])\nprint(&quot;Original Matrix A:\\n&quot;)\nprint(matA)\nprint(&quot;Exponent of the input matrix:\\n&quot;)\nprint(matA ** 2) # finding the exponent of every element of the matrix\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nOriginal Matrix A:\n\n&#x5B;&#x5B;0 1]\n &#x5B;2 3]]\n\nExponent of the input matrix:\n\n&#x5B;&#x5B;0 1]\n &#x5B;4 9]]\n<\/pre><\/div>\n\n\n<p>In the above code snippet, we have found out the exponent of every element of the input matrix by raising it to the power of 2.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Matrix Multiplication Operation using NumPy Methods<\/h2>\n\n\n\n<p>The following techniques can be used to perform NumPy Matrix multiplication:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Using the multiply() method<\/strong><\/li><li><strong>Using the matmul() method<\/strong><\/li><li><strong>Using the dot() method<\/strong> &#8211; Already covered in this article<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Method 1: Using the multiply() method<\/h3>\n\n\n\n<p>The <code>numpy.multiply()<\/code> method performs element-wise multiplication on an input matrix.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as p\n\n\nmatA = p.matrix(&#x5B;&#x5B;10, 20], &#x5B;30, 40]])  \nprint(&#039;MatrixA:\\n&#039;, matA)\n\n\n\nmatB = p.matrix(&#039;&#x5B;10,20;30,40]&#039;, dtype=p.int32)  # Setting the data-type to int\nprint(&#039;\\nMatrixB:\\n&#039;, matB)\n\nprint(&quot;Matrix multplication using numpy.matrix() method&quot;)\nres = p.multiply(matA,matB)\nprint(res)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMatrixA:\n &#x5B;&#x5B;10 20]\n &#x5B;30 40]]\n\nMatrixB:\n &#x5B;&#x5B;10 20]\n &#x5B;30 40]]\nMatrix multplication using numpy.matrix() method\n&#x5B;&#x5B; 100  400]\n &#x5B; 900 1600]]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Method 2: Using the matmul() method<\/h3>\n\n\n\n<p>The <code>numpy.matmul()<\/code> method performs the matrix product on the input matrices.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as p\n\n\nmatA = p.matrix(&#x5B;&#x5B;10, 20], &#x5B;30, 40]])  \nprint(&#039;MatrixA:\\n&#039;, matA)\n\n\n\nmatB = p.matrix(&#039;&#x5B;10,20;30,40]&#039;, dtype=p.int32)  # Setting the data-type to int\nprint(&#039;\\nMatrixB:\\n&#039;, matB)\n\nprint(&quot;Matrix multplication using numpy.matmul() method&quot;)\nres = p.matmul(matA,matB)\nprint(res)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMatrixA:\n &#x5B;&#x5B;10 20]\n &#x5B;30 40]]\n\nMatrixB:\n &#x5B;&#x5B;10 20]\n &#x5B;30 40]]\nMatrix multplication using numpy.matmul() method\n&#x5B;&#x5B; 700 1000]\n &#x5B;1500 2200]]\n<\/pre><\/div>\n\n\n<p>I would strongly recommend all the readers to go through the below tutorial to have a thorough understanding of NumPy Matrix Multiplication: NumPy Matrix Multiplication <\/p>\n\n\n\n<p>  <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">NumPy Matrix Transpose<\/h2>\n\n\n\n<p>The <code>numpy.transpose()<\/code> function performs the transpose on the input matrix and results in a new matrix.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\n \nmatA = numpy.array(&#x5B;numpy.arange(10,15), numpy.arange(15,20)])\nprint(&quot;Original Matrix A:\\n&quot;)\nprint(matA)\nprint(&#039;\\nDimensions of the original MatrixA: &#039;,matA.shape)\nprint(&quot;\\nTranspose of Matrix A:\\n &quot;)\nres = matA.transpose()\nprint(res)\nprint(&#039;\\nDimensions of the Matrix A after performing the Transpose Operation:  &#039;,res.shape)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nOriginal Matrix A:\n\n&#x5B;&#x5B;10 11 12 13 14]\n &#x5B;15 16 17 18 19]]\n\nDimensions of the original MatrixA: (2, 5)\n\nTranspose of Matrix A:\n \n&#x5B;&#x5B;10 15]\n &#x5B;11 16]\n &#x5B;12 17]\n &#x5B;13 18]\n &#x5B;14 19]]\n\nDimensions of the Matrix A after performing the Transpose Operation: (5, 2)\n<\/pre><\/div>\n\n\n<p>Recommended read: NumPy Matrix transpose() function<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the operations performed on Python Matrix and also had a look at the NumPy Matrix operations.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python Matrix<\/li><li><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">NumPy Documentation<\/a><\/li><li>Python NumPy<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We can implement a Python Matrix in the form of a 2-d List or a 2-d Array. To perform operations on Python Matrix, we need to import Python NumPy Module. Python Matrix is essential in the field of statistics, data processing, image processing, etc. Creation of a Python Matrix Python Matrix can be created using [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":3090,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3078","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/3078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=3078"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/3078\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/3090"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=3078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=3078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=3078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}