Q:

Write a NumPy program to add elements in a matrix. If an element in the matrix is 0, we will not add the element below this element

0

Write a NumPy program to add elements in a matrix. If an element in the matrix is 0, we will not add the element below this element

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import numpy as np
def sum_matrix_Elements(m):
    arra = np.array(m)   
    element_sum = 0  
    for p in range(len(arra)):  
        for q in range(len(arra[p])):  
            if arra[p][q] == 0 and p < len(arra)-1: 
                arra[p+1][q] = 0  
            element_sum += arra[p][q]  
    return element_sum
m = [[1, 1, 0, 2],
          [0, 3, 0, 3], 
          [1, 0, 4, 4]]
print("Original matrix:")
print(m)
print("Sum:")
print(sum_matrix_Elements(m))

Sample Output:

Original matrix:
[[1, 1, 0, 2], [0, 3, 0, 3], [1, 0, 4, 4]]
Sum:
14

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now