NumPy: Add elements in a matrix, do not add an element below an element if it is 0
NumPy: Basic Exercise-42 with Solution
Write a NumPy program to add elements to a matrix. If an element in the matrix is 0, we will not add the element below this element.
This problem involves writing a NumPy program to add elements in a matrix with a specific condition: if an element in the matrix is 0, the element directly below it should not be added. The task requires iterating through the matrix and selectively summing elements based on the given condition. This program demonstrates conditional summation within a matrix using NumPy's array manipulation capabilities.
Sample Solution :
Python Code :
# Importing the NumPy library with an alias 'np'
import numpy as np
# Defining a function 'sum_matrix_Elements' that takes a matrix 'm' as input
def sum_matrix_Elements(m):
# Converting the input matrix 'm' into a NumPy array 'arra'
arra = np.array(m)
# Initializing a variable 'element_sum' to store the sum of matrix elements
element_sum = 0
# Looping through rows of the array
for p in range(len(arra)):
# Looping through columns of the array
for q in range(len(arra[p])):
# Checking if the element is 0 and the row index is less than the last row
if arra[p][q] == 0 and p < len(arra)-1:
# Assigning 0 to the element in the next row at the same column index
arra[p+1][q] = 0
# Adding the current element to the total sum
element_sum += arra[p][q]
# Returning the sum of matrix elements
return element_sum
# Initializing a matrix 'm' using a list of lists
m = [[1, 1, 0, 2],
[0, 3, 0, 3],
[1, 0, 4, 4]]
# Printing the original matrix 'm'
print("Original matrix:")
print(m)
# Printing the sum of matrix elements using the 'sum_matrix_Elements' function
print("Sum:")
print(sum_matrix_Elements(m))
Output:
Original matrix: [[1, 1, 0, 2], [0, 3, 0, 3], [1, 0, 4, 4]] Sum: 14
Explanation:
In the above code -
def sum_matrix_Elements(m): This statement defines the function sum_matrix_Elements(m) that takes a nested list (representing a matrix) as its input argument.
arra = np.array(m): This statement converts the input nested list 'm' into a NumPy 2D array named 'arra'.
element_sum = 0: This statement initializes a variable 'element_sum' to store the sum of the matrix elements, initially set to 0.
The nested for loops (for p in range(len(arra)): and for q in range(len(arra[p])):) iterate over each element in the 2D array 'arra'.
if arra[p][q] == 0 and p < len(arra)-1:: This statement checks if the current element 'arra[p][q]' is 0 and if it's not in the last row of the array.
arra[p+1][q] = 0: If the above condition is True, it sets the element below the current element to 0.
element_sum += arra[p][q]: This statement adds the current element value to the 'element_sum'.
return element_sum: This statement returns the calculated sum of the matrix elements.
Python-Numpy Code Editor:
Previous: NumPy program to convert numpy dtypes to native python types.
Next: NumPy program to find the missing data in a given array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/numpy/basic/numpy-basic-exercise-42.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics