Question: Complete the function below that returns the sum of all rows in a 2-D matrix as a row vector. For example, given matrix array([[ 0,

Complete the function below that returns the sum of all rows in a 2-D matrix as a row vector. For example, given matrix

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       ...
       [90, 91, 92, 93, 94],
       [95, 96, 97, 98, 99]])
it should return

array([ 950,  970,  990, 1010, 1030])
This function should return a np.ndarray of shape (arr.shape[0], ) or (-1, ), given the input 2D matrix arr.
Note that -1 is a kind of wildcard or inferred value in the numpy (re)shaping context.

 

 

def calc_row_sum(arr):

""" This function calculates the sum of all rows in a 2D matrix "arr". """

     row_sum = None

# YOUR CODE HERE

#raise NotImplementedError()

return row_sum

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres one way to implement calcrowsum import numpy as np def calcrowsumarr Use NumPys builtin sum fu... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!