Write a Numpy program to calculate column-wise sum of a given matrix.
Source Code:
import numpy as np
mtrx = np.array([],dtype=int)
rows, cols = map(int,input("Enter number of rows and columns:").split())
for i in range(rows):
print("Enter {} elements for {} row:".format(cols,i))
ary = np.array(list(map(int,input().split()))[:cols])
if len(mtrx)==0:
mtrx = np.append(mtrx,ary)
else:
mtrx = np.stack((mtrx,ary))
colSum = np.sum(mtrx,axis=0)
print("Result is:\n",colSum)
Sample Output:mtrx = np.array([],dtype=int)
rows, cols = map(int,input("Enter number of rows and columns:").split())
for i in range(rows):
print("Enter {} elements for {} row:".format(cols,i))
ary = np.array(list(map(int,input().split()))[:cols])
if len(mtrx)==0:
mtrx = np.append(mtrx,ary)
else:
mtrx = np.stack((mtrx,ary))
colSum = np.sum(mtrx,axis=0)
print("Result is:\n",colSum)
Enter number of rows and columns:2 3
Enter 3 elements for 0 row:
1 2 3
Enter 3 elements for 1 row:
4 5 6
Result is:
[5 7 9]
Enter 3 elements for 0 row:
1 2 3
Enter 3 elements for 1 row:
4 5 6
Result is:
[5 7 9]