Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the maximum element of each row in a matrix using Python
In this article, we will learn a python program to find the maximum element of each row in a matrix.
Assume we have taken an NxN input matrix. We will now find the maximum element of each row in an input matrix using the below methods.
Methos Used
The following are the various methods to accomplish this task ?
Using Nested For Loops
Using max() function
Using map(),lambda functions
Method 1: Using Nested For Loops
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task. ?
Create a function maximumRowElement() to print the maximum element of each row of an input matrix by accepting the input matrix as an argument.
Get the number of rows of the matrix by calculating the length of the matrix using the len() function.
Get the number of columns of the matrix by calculating the length of any row of the matrix.
Traverse through the rows of the matrix using for loop
Take a variable to store the maximum row element and initialize it to 0.
Traverse through all the columns of a matrix using another nested for loop.
Check whether the current element value is greater than the above max variable.
If the above condition is true then set this element as the maximum value.
Print the maximum value variable.
Create a variable to store the input matrix and print the given matrix using nested for loops.
Call the above-defined maximumRowElement() function by passing the input matrix as an argument to print the maximum element of each row in a matrix.
Example
The following program returns the maximum element of each row of an input matrix using the nested for loop ?
# creatind a function to get the maximum element in a
# row of a matrix by accepting the input matrix as an argument
def maximumRowElement(inputMatrix):
# getting the number of rows of the given matrix
rows = len(inputMatrix)
# getting the number of columns of the given matrix
cols = len(inputMatrix[0])
# traversing through the rows of the matrix
for p in range(rows):
# initializing maximum with 0 for finding a maximum element of each row
maximum = 0
# traversing through all the columns of a matrix
for q in range(cols):
# checking whether the current element value is greater than the max value
if inputMatrix[p][q] > maximum:
# assigning that element to the maximum now it becomes the maximum element
maximum = inputMatrix[p][q]
# printing the maximum element of each row of a matrix
print(maximum)
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3, 4],
[6, 10, 8, 11],
[7, 2, 4, 3],
[1, 2, 3, 4]]
print("The Given Matrix is:")
# traversing through the rows of a matrix
for m in range(len(inputMatrix)):
# traversing through all the columns of the current row
for n in range(len(inputMatrix[0])):
# printing the corresponding element
print(inputMatrix[m][n], end=' ')
print()
print("maximum element in each row of an input matrix is:")
# calling the maximumRowElement() function by passing
# input matrix to it
maximumRowElement(inputMatrix)
Output
On executing, the above program will generate the following output ?
The Given Matrix is: 5 1 3 4 6 10 8 11 7 2 4 3 1 2 3 4 maximum element in each row of an input matrix is: 5 11 7 4
Method 2: Using max() Function
The max() method(returns the highest-valued item/greatest numbers in an iterable)
Example
The following program returns the maximum element of each row of an input matrix using the max() function ?
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3, 4],
[6, 10, 8, 11],
[7, 2, 4, 3],
[1, 2, 3, 4]]
print("The Given Matrix is:")
# traversing through the rows of a matrix
for m in range(len(inputMatrix)):
for n in range(len(inputMatrix[0])):
# printing the corresponding element
print(inputMatrix[m][n], end=' ')
print()
print("maximum element in each row of an input matrix is:")
# traversing through each row of elements of the matrix
for k in inputMatrix:
# printing the maximum element in a row using the max() function
print(max(k))
Output
The Given Matrix is: 5 1 3 4 6 10 8 11 7 2 4 3 1 2 3 4 maximum element in each row of an input matrix is: 5 11 7 4
Method 3: Using map(), Lambda Functions
Using the built-in map() function is another method that can be used to get the largest element in each row of a matrix. The map() method applies a specified function on each element of an iterable in a definite way (such as a list or a matrix). The map() method may be used in this situation to apply the max() function to each row of the matrix.
Example
The following program returns the maximum element of each row of an input matrix using the map(),lambda functions ?
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3, 4],
[6, 10, 8, 11],
[7, 2, 4, 3],
[1, 2, 3, 4]]
print("The Given Matrix is:")
# traversing through the rows of a matrix
for m in range(len(inputMatrix)):
# traversing through all the columns of the current row
for n in range(len(inputMatrix[0])):
# printing the corresponding element
print(inputMatrix[m][n], end=' ')
print()
print("maximum element in each row of an input matrix is:")
# Applying max() function to each list element(row) to get maximum value
result = list(map(lambda row: max(row), inputMatrix))
# Traversing in the result list
for i in result:
# Printing the maximum row element
print(i)
Output
The Given Matrix is: 5 1 3 4 6 10 8 11 7 2 4 3 1 2 3 4 maximum element in each row of an input matrix is: 5 11 7 4
Conclusion
In this article, we learned three different approaches of printing each row's highest value in the given matrix. We also learned how to apply a certain condition to each iterable using the map() function.