Find the first row of a matrix containing all the positive elements and the sum of these elements. Reduce all elements of this matrix to this sum.
To achieve the required, you should find the array that has all the positive elements, save the index of this array and open another loop to add them. How could I do it, that is, how to find this array and save it?
The objective is to obtain the row of the matrix that has all the positive elements, in this case it would be [7,2,3] and add these elements, which would be 12, and replace all the elements of the matrix with the value of the sum
This is my intent, in this way add all the positive elements.
matrix = [[-5, -6, 2], [7, 2, 3], [8, 4, -9]]
summ = 0
for i in range(len(matrix)):
pos = False
for j in range(len(matrix[i])):
if matrix[i][j] > 0:
pos = True
summ += matrix[i][j]
if pos:
for i in range(len(matrix)):
for j in range(len(matrix[i])):
matrix[i][j] = summ
print("Suma: ", summ)
for i in matrix:
print(" ",i)
else:
print("No hay filas con todos elementos positivos.")