{"id":17235,"date":"2022-05-18T09:35:34","date_gmt":"2022-05-18T09:35:34","guid":{"rendered":"https:\/\/linuxways.net\/?p=17235"},"modified":"2022-05-18T09:35:34","modified_gmt":"2022-05-18T09:35:34","slug":"how-to-make-a-matrix-in-python","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/how-to-make-a-matrix-in-python\/","title":{"rendered":"How to Make a Matrix in Python"},"content":{"rendered":"<p>A matrix is an array having exactly two-dimensions and this concept is very important in mathematics. All elements in a matrix are organized into rows and columns. For example, the matrix given below has 2 rows and 3 columns and is pronounced as &#8220;two by three&#8221; matrix.<\/p>\n<p>As there is no built-in data type for matrices in python so we can create matrix by following methods:<\/p>\n<ol>\n<li>By using nested lists<\/li>\n<li>By using numpy array function<\/li>\n<li>By using numpy matrix function<\/li>\n<li>By using numpy reshape function<\/li>\n<li>By taking input from user<\/li>\n<\/ol>\n<h2>1. By using Nested Lists<\/h2>\n<p>We can use a nested list to create a matrix.<\/p>\n<pre>A = [[5, 1, 8], [7, 5, 3], [4, 6, 3]]\r\n\r\nfor i in A:\r\n\r\nprint(i)<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"325\" height=\"67\" class=\"wp-image-17236\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/a-picture-containing-text-screen-gauge-device.png\" alt=\"A picture containing text, screen, gauge, device Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/a-picture-containing-text-screen-gauge-device.png 325w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/a-picture-containing-text-screen-gauge-device-300x62.png 300w\" sizes=\"auto, (max-width: 325px) 100vw, 325px\" \/><\/strong><\/p>\n<p>Figure 1: Using nested list<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"97\" height=\"73\" class=\"wp-image-17237\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/a-picture-containing-text-outdoor-description-au.png\" alt=\"A picture containing text, outdoor Description automatically generated\" \/><\/strong><\/p>\n<p>Figure 2: Output<\/p>\n<p>In figure 1, we have three sub-lists inside the main list. We iterate through every row individually using for loop as displayed in figure 2. We did not print the list directly because that will print the list in a single line.<\/p>\n<h2>2. By using numpy Array Function<\/h2>\n<p>We can create a matrix using the numpy.array() function in Python.<\/p>\n<pre>import numpy as np\r\n\r\nA = np.array([[1, 2, 3], [3, 4, 5]])\r\n\r\nprint(A)\r\n\r\nprint('No. of Dimensions:', A.ndim)\r\n\r\nprint('Dimensions:', A.shape)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"316\" height=\"109\" class=\"wp-image-17238\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-18.png\" alt=\"Text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-18.png 316w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-18-300x103.png 300w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><\/p>\n<p>Figure 3: Using numpy.array() function<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"188\" height=\"89\" class=\"wp-image-17239\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-19.png\" alt=\"Text Description automatically generated\" \/><\/p>\n<p>Figure 4: Output<\/p>\n<p>In figure 3, we import numpy module as np and pass np.array function as a nested list which creates a matrix, whereas its dimension can be seen in figure 4 containing 2 rows and 3 columns.<\/p>\n<h2>3. By using numpy Matrix Function<\/h2>\n<p>Matrix function of numpy module returns a matrix from an array-like object, or from a string of data. In python matrix objects inherit all the attributes and methods of ndarray objects.<\/p>\n<h3>Example 1<\/h3>\n<pre>import numpy as np\r\n\r\nA= np.matrix([[1, 2, 6], [3, 4, 8],[4,8,9]])\r\n\r\nprint(A)\r\n\r\nprint(type(A))<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"379\" height=\"90\" class=\"wp-image-17240\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-20.png\" alt=\"Text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-20.png 379w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-20-300x71.png 300w\" sizes=\"auto, (max-width: 379px) 100vw, 379px\" \/><\/strong><\/p>\n<p>Figure 5: Using numpy.matrix() function for array input<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"218\" height=\"99\" class=\"wp-image-17241\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-21.png\" alt=\"Text Description automatically generated\" \/><\/strong><\/p>\n<p>Figure 6: Output<\/p>\n<p>In figure 5, we import numpy module as np and pass np.matrix function a nested list. A 3\u00d73 matrix is created which belongs to numpy.matrix class.<\/p>\n<h3>Example 2<\/h3>\n<pre>import numpy as np\r\n\r\nA = np.matrix('4 7 8; 7 9 1')\r\n\r\nprint(A)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"264\" height=\"77\" class=\"wp-image-17242\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-22.png\" alt=\"Text Description automatically generated\" \/><\/p>\n<p>Figure 7: Using numpy.matrix() function for string input<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"103\" height=\"47\" class=\"wp-image-17243\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/word-image-47.png\" \/><\/strong><\/p>\n<p>Figure 8: Output<\/p>\n<p>In the example 2, we have passed numpy.matrix function as a string matrix and we have separated the columns with commas or spaces whereas semicolons separating the rows. The output matrix is having 2 rows and 3 columns as shown in figure 8.<\/p>\n<h2>4. By using numpy Reshape Function<\/h2>\n<p>The numpy.reshape() can also be used to create matrix and this function alters the shape of the array so we can use it to change the shape of a 1-D array to a 2-D array without changing elements. We must need to check the number of elements in an array before changing its shape.<\/p>\n<pre>import numpy as np\r\n\r\nA = np.array([[4,1,8,7,5,7,9,6,8]]).reshape(3,3)\r\n\r\nprint(\"Matrix= \\n\", A)\r\n\r\nprint(\"Dimensions = \", A.shape)<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"433\" height=\"87\" class=\"wp-image-17244\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/a-screenshot-of-a-computer-description-automatica-1.png\" alt=\"A screenshot of a computer Description automatically generated with medium confidence\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/a-screenshot-of-a-computer-description-automatica-1.png 433w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/a-screenshot-of-a-computer-description-automatica-1-300x60.png 300w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/strong><\/p>\n<p>Figure 9: Using numpy.reshape() function<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"169\" height=\"109\" class=\"wp-image-17245\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-23.png\" alt=\"Text Description automatically generated\" \/><\/strong><\/p>\n<p>Figure 10: Output<\/p>\n<p>In the above example, we have a 1-dimensional array consisting of nine elements, whereas The reshape() function altered this to a 2-dimensional array thus the elements are arranged with the dimension of 3&#215;3 having 3 rows and 3 columns.<\/p>\n<h2>5. By taking Input from the User<\/h2>\n<p>In Python, we can create a matrix taking input from user as well.<\/p>\n<pre>row = int(input(\"enter the number of rows:\"))\r\n\r\ncol = int(input(\"enter the number of columns:\"))\r\n\r\n# Initialize matrix\r\n\r\nm = []\r\n\r\nprint(\"enter the entries row wise:\")\r\n\r\n# For user input\r\n\r\nfor i in range(row): # A for loop for row entries\r\n\r\na = []\r\n\r\nfor j in range(col): # A for loop for column entries\r\n\r\na.append(int(input()))\r\n\r\nm.append(a)\r\n\r\n# For printing the matrix\r\n\r\nfor i in range(row):\r\n\r\nfor j in range(col):\r\n\r\nprint(m[i][j], end=\" \")\r\n\r\nprint()<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"502\" height=\"418\" class=\"wp-image-17246\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-24.png\" alt=\"Text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-24.png 502w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-24-300x250.png 300w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><br \/>\nFigure 11: Using input method<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"290\" height=\"262\" class=\"wp-image-17247\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/05\/text-description-automatically-generated-25.png\" alt=\"Text Description automatically generated\" \/><\/strong><\/p>\n<p>Figure 12: Output<\/p>\n<p>In the above example, we take a number of rows and columns as input from the user. We declared an empty list \u2018m\u2019. Using nested for loop elements are added for row and column to \u2018m\u2019 list whereas the elements of the matrix are printed shown in figure 12.<\/p>","protected":false},"excerpt":{"rendered":"<p>A matrix is an array having exactly two-dimensions and this concept is very important in mathematics. All elements in a matrix are organized into rows and columns. For&hellip;<\/p>","protected":false},"author":1,"featured_media":17286,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[1009,10],"class_list":["post-17235","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-matrix","tag-python"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=17235"}],"version-history":[{"count":1,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17235\/revisions"}],"predecessor-version":[{"id":17248,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17235\/revisions\/17248"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/17286"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=17235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=17235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=17235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}