How to find the sum of rows and columns of a given matrix using numpy in Python

1 Answer

0 votes
import numpy as np

matrix = np.matrix([[1, 2, 3], [4, 4, 6]])
print(matrix)

sum_of_rows = np.sum(matrix, axis = 1)
print("Sum of rows =\n", sum_of_rows)

sum_of_cols = np.sum(matrix, axis = 0)
print("Sum of columns =", sum_of_cols)



'''
run:

[[1 2 3]
 [4 4 6]]
Sum of rows =
 [[ 6]
 [14]]
Sum of columns = [[5 6 9]]

'''

 



answered Jun 24, 2023 by avibootz

Related questions

1 answer 136 views
1 answer 196 views
1 answer 255 views
255 views asked Jan 25, 2019 by avibootz
1 answer 142 views
1 answer 162 views
...