Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Program to Find maximum element of each row in a matrix

  • Last Updated : 20 Jan, 2022

Given a matrix, the task is to find the maximum element of each row.
Examples: 
 

Input :  [1, 2, 3]
         [1, 4, 9]
         [76, 34, 21]

Output :
3
9
76

Input : [1, 2, 3, 21]
        [12, 1, 65, 9]
        [1, 56, 34, 2]
Output :
21
65
56

 

Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each element inside the row and find for the maximum element. Finally, print the element. 
  
Below is the implementation : 
 

Python




# Python program to find maximum 
# element of each row in a matrix
  
# importing numpy
import numpy
  
# Function to get max element
def maxelement(arr):
      
    # get number of rows and columns
    no_of_rows = len(arr)
    no_of_column = len(arr[0])
      
    for i in range(no_of_rows):
          
        # Initialize max1 to 0 at beginning
        # of finding max element of each row
        max1 = 0
        for j in range(no_of_column):
            if arr[i][j] > max1 :
                max1 = arr[i][j]
                  
        # print maximum element of each row
        print(max1)
  
# Driver Code
arr = [[3, 4, 1, 8],
       [1, 4, 9, 11],
       [76, 34, 21, 1],
       [2, 1, 4, 5]]
  
# Calling the function        
maxelement(arr)

Output :

8
11
76
5

Please refer complete article on Find maximum element of each row in a matrix for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!