nzmask = ixs != 0
nzsum = np.bincount(ixs[nzmask] - 1, minlength=mat.shape[0] - 1).dot(mat[1:])
row0_sum = mat[0] * (len(ixs) - np.count_nonzero(nzmask))
out = nzsum + row0_sum
numpy: efficiently add rows of a matrix