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 is a number that appears in a matrix.

**Example:**

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.

**Examples:**

**Example1:**

**Input:**

Given Matrix 2Â Â 9Â Â 1 11 4Â Â 5 9Â Â 2Â Â 3 1Â Â 2Â Â 3

**Output:**

The maximum element of the given matrix [[2, 9, 1], [11, 4, 5], [9, 2, 3], [1, 2, 3]] is : 11

**Example2:**

**Input:**

Given Matrix 1 7 5 25 4 6

**Output:**

The maximum element of the given matrix [[1, 7, 5], [25, 4, 6]] is : 25

## Program to Find the Maximum Element in the Matrix in Python

Below are the ways to find the maximum element in the given Matrix in Python

### Method #1: Using For Loop (Static Input)

**Approach:**

- Give the matrix as static input and store it in a variable.
- Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
- Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
- Take a variable
**mtrxmax**which stores the maximum element of the given matrix and initialize its value to the first element of the given matrix. - Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
- Check if the
**Â gvnmatrix[n][m]**value (where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop) is greater thanÂ**mtrxmaxÂ**using the if conditional statement. - If it is true then modify the
**mtrxmaxÂ**value withÂ**gvnmatrix[n][m]**value. - Print the
**mtrxmax**value which is the maximum element of the given Matrix - The Exit of the Program.

**Below is the implementation:**

# Give the matrix as static input and store it in a variable. mtrx = [[2, 9, 1], [11, 4, 5], [9, 2, 3], [1, 2, 3]] # Calculate the number of rows of the given matrix by # calculating the length of the nested list using the len() function # and store it in a variable mtrxrows. mtrxrows = len(mtrx) # Calculate the number of columns of the given matrix by # calculating the length of the first list in the nested list # using the len() function and store it in a variable mtrxcols. mtrxcols = len(mtrx[0]) # Take a variable mtrxmax which stores the maximum element of the given matrix # and initialize its value to the first element of the given matrix. mtrxmax = mtrx[0][0] # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of rows using another # Nested For loop(Inner For loop). for m in range(mtrxcols): # Check if theÂ gvnmatrix[n][m] value (where n is the iterator value of the # parent For loop and m is the iterator value of the inner For loop) # is greater thanÂ mtrxmaxÂ using the if conditional statement. if(mtrx[n][m] > mtrxmax): # If it is true then modify the mtrxmaxÂ value withÂ gvnmatrix[n][m] value. mtrxmax = mtrx[n][m] # Print the mtrxmax value which is the maximum element of the given Matrix print('The maximum element of the given matrix', mtrx, 'is :') print(mtrxmax)

**Output:**

The maximum element of the given matrix [[2, 9, 1], [11, 4, 5], [9, 2, 3], [1, 2, 3]] is : 11

### Method #2: Using For loop (User Input)

**Approach:**

- Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
- Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
- Take a list and initialize it with an empty value using [] or list() to say
**gvnmatrix**. - Loop till the given number of rows using the For loop
- Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
- Add the above row elements list to
**gvnmatrix**using the append() function. - Take a variable
**mtrxmax**which stores the maximum element of the given matrix and initialize its value to the first element of the given matrix. - Loop till the given number of rows using the For loop.
- Inside the For loop, Iterate till the given number of rows using another Nested For loop(Inner For loop).
- Check if the
**Â gvnmatrix[n][m]**value (where n is the iterator value of the parent For loop and m is the iterator value of the inner For loop) is greater thanÂ**mtrxmaxÂ**using the if conditional statement. - If it is true then modify the
**mtrxmaxÂ**value withÂ**gvnmatrix[n][m]**value. - Print the
**mtrxmax**value which is the maximum element of the given Matrix - The Exit of the Program.

**Below is the implementation:**

# Give the number of rows of the matrix as user input using the int(input()) function # and store it in a variable. mtrxrows = int(input('Enter some random number of rows of the matrix = ')) # Give the number of columns of the matrix as user input using the int(input()) function # and store it in another variable. mtrxcols = int(input('Enter some random number of columns of the matrix = ')) # Take a list and initialize it with an empty value using [] or list() to say gvnmatrix. mtrx = [] # Loop till the given number of rows using the For loop for n in range(mtrxrows): # Inside the For loop, Give all the row elements of the given Matrix as a list using # the list(),map(),int(),split() functions and store it in a variable. l = list(map(int, input( 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split())) # Add the above row elements list to gvnmatrix using the append() function. mtrx.append(l) # Take a variable mtrxmax which stores the maximum element of the given matrix # and initialize its value to the first element of the given matrix. mtrxmax = mtrx[0][0] # Loop till the given number of rows using the For loop. for n in range(mtrxrows): # Inside the For loop, Iterate till the given number of rows using another # Nested For loop(Inner For loop). for m in range(mtrxcols): # Check if theÂ gvnmatrix[n][m] value (where n is the iterator value of the # parent For loop and m is the iterator value of the inner For loop) # is greater thanÂ mtrxmaxÂ using the if conditional statement. if(mtrx[n][m] > mtrxmax): # If it is true then modify the mtrxmaxÂ value withÂ gvnmatrix[n][m] value. mtrxmax = mtrx[n][m] # Print the mtrxmax value which is the maximum element of the given Matrix print('The maximum element of the given matrix', mtrx, 'is :') print(mtrxmax)

**Output:**

Enter some random number of rows of the matrix = 2 Enter some random number of columns of the matrix = 3 Enter {3} elements of row {1} separated by spaces = 1 7 5 Enter {3} elements of row {2} separated by spaces = 25 4 6 The maximum element of the given matrix [[1, 7, 5], [25, 4, 6]] is : 25

Explore more instances related to python concepts fromÂ Python Programming ExamplesÂ Guide and get promoted from beginner to professional programmer level in Python Programming Language.