{"id":19483,"date":"2021-08-30T16:06:13","date_gmt":"2021-08-30T10:36:13","guid":{"rendered":"https:\/\/python-programs.com\/?p=19483"},"modified":"2021-11-22T18:37:08","modified_gmt":"2021-11-22T13:07:08","slug":"python-program-to-find-the-maximum-element-in-the-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-maximum-element-in-the-matrix\/","title":{"rendered":"Python Program to Find the Maximum Element in the Matrix"},"content":{"rendered":"
In the previous article, we have discussed Python Program to find the Sum of Series 1^2+2^2+3^2…+N^2<\/a> 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 a 5*4 matrix.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the maximum element in the given Matrix in Python<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/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 In the previous article, we have discussed Python Program to find the Sum of Series 1^2+2^2+3^2…+N^2 Given a matrix, the task is to find the maximum element in the given Matrix in Python What is a matrix: A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry …<\/p>\n
\nGiven a matrix, the task is to find the maximum element in the given Matrix in Python<\/p>\nGiven Matrix\r\n2\u00a0 \u00a09\u00a0 \u00a01\r\n11 4\u00a0 \u00a05 \r\n9\u00a0 \u00a02\u00a0 \u00a03\r\n1\u00a0 \u00a02\u00a0 \u00a03<\/pre>\n
The maximum element of the given matrix [[2, 9, 1], [11, 4, 5], [9, 2, 3], [1, 2, 3]] is :\r\n11<\/pre>\n
Given Matrix \r\n1 7 5\r\n25 4 6<\/pre>\n
The maximum element of the given matrix [[1, 7, 5], [25, 4, 6]] is :\r\n25<\/pre>\n
Program to Find the Maximum Element in the Matrix in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the matrix as static input and store it in a variable.\r\nmtrx = [[2, 9, 1], [11, 4, 5], [9, 2, 3], [1, 2, 3]]\r\n# Calculate the number of rows of the given matrix by\r\n# calculating the length of the nested list using the len() function\r\n# and store it in a variable mtrxrows.\r\nmtrxrows = len(mtrx)\r\n# Calculate the number of columns of the given matrix by\r\n# calculating the length of the first list in the nested list\r\n# using the len() function and store it in a variable mtrxcols.\r\nmtrxcols = len(mtrx[0])\r\n# Take a variable mtrxmax which stores the maximum element of the given matrix\r\n# and initialize its value to the first element of the given matrix.\r\nmtrxmax = mtrx[0][0]\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of rows using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Check if the\u00a0gvnmatrix[n][m] value (where n is the iterator value of the\r\n # parent For loop and m is the iterator value of the inner For loop)\r\n # is greater than\u00a0mtrxmax\u00a0using the if conditional statement.\r\n if(mtrx[n][m] > mtrxmax):\r\n # If it is true then modify the mtrxmax\u00a0value with\u00a0gvnmatrix[n][m] value.\r\n mtrxmax = mtrx[n][m]\r\n# Print the mtrxmax value which is the maximum element of the given Matrix\r\nprint('The maximum element of the given matrix', mtrx, 'is :')\r\nprint(mtrxmax)\r\n<\/pre>\n
The maximum element of the given matrix [[2, 9, 1], [11, 4, 5], [9, 2, 3], [1, 2, 3]] is :\r\n11<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the number of rows of the matrix as user input using the int(input()) function\r\n# and store it in a variable.\r\nmtrxrows = int(input('Enter some random number of rows of the matrix = '))\r\n# Give the number of columns of the matrix as user input using the int(input()) function\r\n# and store it in another variable.\r\nmtrxcols = int(input('Enter some random number of columns of the matrix = '))\r\n# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.\r\nmtrx = []\r\n# Loop till the given number of rows using the For loop\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Give all the row elements of the given Matrix as a list using\r\n # the list(),map(),int(),split() functions and store it in a variable.\r\n l = list(map(int, input(\r\n 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))\r\n # Add the above row elements list to gvnmatrix using the append() function.\r\n\r\n mtrx.append(l)\r\n# Take a variable mtrxmax which stores the maximum element of the given matrix\r\n# and initialize its value to the first element of the given matrix.\r\nmtrxmax = mtrx[0][0]\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of rows using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Check if the\u00a0gvnmatrix[n][m] value (where n is the iterator value of the\r\n # parent For loop and m is the iterator value of the inner For loop)\r\n # is greater than\u00a0mtrxmax\u00a0using the if conditional statement.\r\n if(mtrx[n][m] > mtrxmax):\r\n # If it is true then modify the mtrxmax\u00a0value with\u00a0gvnmatrix[n][m] value.\r\n mtrxmax = mtrx[n][m]\r\n# Print the mtrxmax value which is the maximum element of the given Matrix\r\nprint('The maximum element of the given matrix', mtrx, 'is :')\r\nprint(mtrxmax)\r\n<\/pre>\n
Enter some random number of rows of the matrix = 2\r\nEnter some random number of columns of the matrix = 3\r\nEnter {3} elements of row {1} separated by spaces = 1 7 5\r\nEnter {3} elements of row {2} separated by spaces = 25 4 6\r\nThe maximum element of the given matrix [[1, 7, 5], [25, 4, 6]] is :\r\n25<\/pre>\n
\n