To implement a = (b / x[:, np.newaxis]).sum(axis=1) , you can use a = b.sum(axis=1).A1 / x . The A1 attribute returns 1D ndarray, so the result is 1D ndarray, not matrix . This squeezed expression works because you both scale on x and sum along axis 1. For example:
In [190]: b Out[190]: <3x3 sparse matrix of type '<type 'numpy.float64'>' with 5 stored elements in Compressed Sparse Row format> In [191]: bA Out[191]: array([[ 1., 0., 2.], [ 0., 3., 0.], [ 4., 0., 5.]]) In [192]: x Out[192]: array([ 2., 3., 4.]) In [193]: b.sum(axis=1).A1 / x Out[193]: array([ 1.5 , 1. , 2.25])
More generally, if you want to scale the rows of a sparse matrix with the vector x , you can multiply b left with a sparse matrix containing 1.0/x on the diagonal. The scipy.sparse.spdiags function can be used to create such a matrix. For instance:
In [71]: from scipy.sparse import csc_matrix, spdiags In [72]: b = csc_matrix([[1,0,2],[0,3,0],[4,0,5]], dtype=np.float64) In [73]: bA Out[73]: array([[ 1., 0., 2.], [ 0., 3., 0.], [ 4., 0., 5.]]) In [74]: x = array([2., 3., 4.]) In [75]: d = spdiags(1.0/x, 0, len(x), len(x)) In [76]: dA Out[76]: array([[ 0.5 , 0. , 0. ], [ 0. , 0.33333333, 0. ], [ 0. , 0. , 0.25 ]]) In [77]: p = d * b In [78]: pA Out[78]: array([[ 0.5 , 0. , 1. ], [ 0. , 1. , 0. ], [ 1. , 0. , 1.25]]) In [79]: a = p.sum(axis=1) In [80]: a Out[80]: matrix([[ 1.5 ], [ 1. ], [ 2.25]])