

{"id":123590,"date":"2023-11-04T12:03:56","date_gmt":"2023-11-04T06:33:56","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123590"},"modified":"2024-02-28T12:05:26","modified_gmt":"2024-02-28T06:35:26","slug":"python-program-on-read-data-in-matrix","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/","title":{"rendered":"Python Program on Read Data in Matrix"},"content":{"rendered":"<p>In this article, we will explore two Python programs that involve the creation of matrices using the NumPy library. Matrices play a crucial role in various scientific and computational applications, and NumPy provides efficient tools for working with them. The first program demonstrates how to create a matrix by taking runtime input for each element, while the second program creates a matrix by taking a string of elements as input.<\/p>\n<h2>Prerequisites<\/h2>\n<ul>\n<li>Fundamental Python Knowledge (Variables, Data Types, Syntax)<\/li>\n<li>Basic Familiarity with the NumPy Library (Numerical Computing, Basic Array Operations)<\/li>\n<\/ul>\n<h3>Topic Explanation<\/h3>\n<h4>Program 1: Create Matrix with Runtime Element Input<\/h4>\n<p>The first program prompts the user to enter the number of rows and columns for the matrix. It then takes runtime input for each element of the matrix and uses NumPy to reshape the array into a matrix. The program also showcases the creation of a matrix object using np.matrix() and calculates its transpose.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Program for Creating a Matrix using Runtime input and finding its transpose\r\nimport numpy as np\r\nimport array as ar\r\n\r\n# Get the number of rows and columns for the matrix from the user\r\nm, n = [int(a) for a in input(\"Input the row and column numbers: \").split()]\r\n\r\n# Initialize an array to store matrix elements\r\nmyar1 = ar.array('i', [])\r\n\r\n# Determine how many elements there are in the matrix overall.\r\nx = m * n\r\n\r\n# Get user input for each element in the matrix\r\nprint(f\"Enter {x} elements in the matrix:\")\r\nfor i in range(x):\r\n    a = int(input())\r\n    myar1.append(a)\r\n\r\n# Reshape the 1D array to create a 2D array representing the matrix\r\nmyar2 = np.reshape(myar1, (m, n))\r\n\r\n# Create a matrix using the NumPy library\r\nmt = np.matrix(myar2)\r\n\r\n# Display the original matrix\r\nprint(\"Original Matrix:\")\r\nprint(mt)\r\n\r\n# Display the transpose of the matrix\r\nprint(\"Transpose is:\")\r\nprint(mt.transpose())<\/pre>\n<div class=\"df-code-out\">\n<p><strong>Output:<\/strong><\/p>\n<p>Input the row and column numbers3 3<br \/>\nEnter 9 elements in matrix<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n1<br \/>\n[[1 1 1]<br \/>\n[1 1 1]<br \/>\n[1 1 1]]<br \/>\n<strong>Transpose is :<\/strong><br \/>\n[[1 1 1]<br \/>\n[1 1 1]<br \/>\n[1 1 1]]<\/p>\n<\/div>\n<h4>Code Explanation:<\/h4>\n<p>Import numpy and array modules<\/p>\n<ul>\n<li>Take row and column input from user and convert to ints<\/li>\n<li>Create an empty integer array using array module<\/li>\n<li>Calculate total elements as row*column<\/li>\n<li>Print statement to enter the matrix elements<\/li>\n<li>Take input from user in a loop and append to array<\/li>\n<li>Reshape the array to row x column<\/li>\n<li>Convert the array to numpy matrix<\/li>\n<li>Print the matrix<\/li>\n<li>Print the transpose of the matrix using transpose() method<\/li>\n<\/ul>\n<h4>Program 2: Create Matrix with Runtime Element Input by String<\/h4>\n<p>The second program prompts the user to enter the number of rows and columns for the matrix. It then takes a string containing space-separated elements as input and uses NumPy to reshape the string into a matrix. The program demonstrates an alternative approach for creating a matrix using a string of elements.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Program for Creating a Matrix using Runtime input by String\r\nimport numpy as np\r\n\r\n# Ask the user how many rows and columns the matrix will have.\r\nm, n = [int(a) for a in input(\"Input the row and column numbers: \").split()]\r\n\r\n# Determine how many elements there are in the matrix overall.\r\nx = m * n\r\n\r\n# Get a single string containing all elements separated by spaces\r\ninput_str = input(f\"Enter {x} elements separated by spaces: \")\r\n\r\n# Create a matrix by reshaping a 1D array obtained from the input string\r\nmt = np.reshape(np.matrix(input_str), (m, n))\r\n\r\n# Display the created matrix\r\nprint(\"Matrix:\")\r\nprint(mt)<\/pre>\n<div class=\"df-code-out\">\n<p><strong>Output:<\/strong><\/p>\n<p><strong>Input the row and column numbers:<\/strong> 3 3<br \/>\n<strong>Enter 9 elements separated by spaces:<\/strong> 5 5 5 5 5 5 5 5 5<br \/>\n<strong>Matrix:<\/strong><br \/>\n[[5 5 5]<br \/>\n[5 5 5]<br \/>\n[5 5 5]]<\/p>\n<\/div>\n<h4>Code Explanation:<\/h4>\n<ul>\n<li>Import the numpy library to support matrix operations<\/li>\n<li>Take row and column size input from user as two integer values separated by space<\/li>\n<li>Convert the input to ints and assign to variables m and n<\/li>\n<li>Calculate total elements as m*n and store in x<\/li>\n<li>Take a string input from user containing x no. of elements separated by space<\/li>\n<li>Convert the string into a numpy matrix using np.matrix()<\/li>\n<li>Reshape the 1D matrix into 2D matrix with dimensions m x n using np.reshape()<\/li>\n<li>Print the final matrix mt<\/li>\n<\/ul>\n<h3>Summary<\/h3>\n<p>In summary, these Python programs serve as practical examples of matrix creation using runtime input with the NumPy library. Understanding these techniques is essential for tasks involving dynamic matrix generation and manipulation in Python. Proficiency in this area equips individuals with the skills needed to adapt to real-world data and computational challenges across various domains, such as scientific simulations, data-driven applications, and machine learning algorithms.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore two Python programs that involve the creation of matrices using the NumPy library. Matrices play a crucial role in various scientific and computational applications, and NumPy provides efficient&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[28701,10333,28626,28700],"class_list":["post-123590","post","type-post","status-publish","format-standard","hentry","category-python","tag-how-to-read-data-at-runtime-in-matrix-in-python","tag-python","tag-python-practical","tag-python-program-on-read-data-in-matrix"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Program on Read Data in Matrix - DataFlair<\/title>\n<meta name=\"description\" content=\"These Python programs serve as practical examples of matrix creation using runtime input with the NumPy library.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Program on Read Data in Matrix - DataFlair\" \/>\n<meta property=\"og:description\" content=\"These Python programs serve as practical examples of matrix creation using runtime input with the NumPy library.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-04T06:33:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-28T06:35:26+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Program on Read Data in Matrix - DataFlair","description":"These Python programs serve as practical examples of matrix creation using runtime input with the NumPy library.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/","og_locale":"en_US","og_type":"article","og_title":"Python Program on Read Data in Matrix - DataFlair","og_description":"These Python programs serve as practical examples of matrix creation using runtime input with the NumPy library.","og_url":"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-04T06:33:56+00:00","article_modified_time":"2024-02-28T06:35:26+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"Python Program on Read Data in Matrix","datePublished":"2023-11-04T06:33:56+00:00","dateModified":"2024-02-28T06:35:26+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/"},"wordCount":481,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["how to read data at runtime in matrix in python","Python","python practical","python program on read data in matrix"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/","url":"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/","name":"Python Program on Read Data in Matrix - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-04T06:33:56+00:00","dateModified":"2024-02-28T06:35:26+00:00","description":"These Python programs serve as practical examples of matrix creation using runtime input with the NumPy library.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-program-on-read-data-in-matrix\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Program on Read Data in Matrix"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=123590"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123590\/revisions"}],"predecessor-version":[{"id":134150,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123590\/revisions\/134150"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}