- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Find maximum element of each row in a matrix?
In this article, the user will understand how to create a Python program to find the maximum element of each row in a matrix in Python. In the first strategy, we cycle through each row and compare the elements to determine the largest value. This strategy offers a fundamental comprehension of the underlying reasoning and is simple to use. The second method makes use of the NumPy module, a well-liked Python tool for scientific computing. We may more quickly and effectively determine the maximum element of each row by making use of NumPy's effective array operations.
Let’s deep delve into this article
Utilizing the naive iteration.
Utilizing the NumPy library.
Let us look into both approaches −
Approach-1: Utilizing the naive iteration
The greatest element in each row of the matrix is determined utilizing a straightforward iterative process in this approach. We monitor the most elements discovered thus far in a variable called maximum by repeatedly iterating over each row and comparing each element. We add the maximum to the list of maximum_elements after iterating through each row. Each row’s maximum element is then compiled into a list by repeating this process for each row in the matrix.
Step 1 − To store the greatest number of elements for each row, initialize the empty list maximum_elements.
Step 2 − Go over each row of the matrix one more time.
Step 3 − Create a variable called maximum and initialize it for each row to hold the most recent element discovered.
Step 4 − Go over each element in the current row one more time.
Step 5 − Compared to maximum_element, compare the current element. Update maximum to reflect this if the current element is larger.
Step 6 − Append maximum to the maximum_elements list after iterating through each element in the row.
Step 7 − For each row, repeat steps 3 to 6.
Step 8 − Return the list of maximum_elements.
#define function def row_max_elements(matrix): maximum_elements =  #iterate elements for row in matrix: maximum = float('-inf') for element in row: if element > maximum: maximum = element maximum_elements.append(maximum) return maximum_elements # An instance usage matrix = [ [8, 2, 3], [4, 5, 0], [10, 8, 11] ] #invoke function maximum_elements = row_max_elements(matrix) print(maximum_elements)
[8, 5, 11]
Approach-2: Utilizing the numpy library.
Utilizing the robust NumPy library, which offers several mathematical and numerical operations on multi-dimensional arrays, is the second method.
The steps to make a background image transparent in python are as follows −
Step 1 − The NumPy library is imported.
Step 2 − The matrix can be initialized directly in the code or read as input.
Step 3 − Find the largest element in each row of the matrix using the NumPy amax function and the axis argument.
Step 4 − The final array is returned.
#import the required module import numpy as np #define function def find_max_elements(matrix): max_elements = np.amax(matrix, axis=1) return max_elements # An instance is matrix = np.array([ [8, 2, 3], [4, 5, 0], [10, 8, 11] ]) max_elements = find_max_elements(matrix) print(max_elements)
[8, 5, 11]
In this article, we looked at two distinct Python solutions to this issue. The initial method used a crude iteration strategy, going through each row iteratively and comparing each element to determine which element was the greatest. The second method made use of the amax function from the NumPy library, which makes use of the array operations power of NumPy to provide a clear and effective answer. You can select the strategy that best meets your needs based on the requirements of your project.
Kickstart Your Career
Get certified by completing the courseGet Started