﻿ Sum Numbers of Each Row of a Matrix Python - ITCodar

# Sum Numbers of Each Row of a Matrix Python

## Sum numbers of each row of a matrix Python

The issue you are having, is that you are already iterating over the elements so it is unnecessary to use it as an index:

``    x = sum(elemento)``

It is generally considered bad form but to iterate over indexes you would use:

``for i in range(len(lista)):    x = sum(lista[i])``

However, without introducing any other modules, you can use `map()` or a simple list comprehension:

``>>> res = list(map(sum, lista))   # You don't need `list()` in Py2>>> print(res)[6, 15, 24]``

Or

``>>> res = [sum(e) for e in lista]>>> print(res)[6, 15, 24]``

## Perform sum over different slice of each row for 2D array

Let's run with your raveling idea. You can convert the indices of your array into raveled indices like this:

``ind = np.stack((start, end), axis=0)ind += np.arange(data.shape[0]) * data.shape[1]ind = ind.ravel(order='F')if ind[-1] == data.size:    ind = ind[:-1]``

Now you can ravel the original array, and `add.reduceat` on the segments thus defined:

``np.add.reduceat(data.ravel(), ind)[::2] / np.subtract(end, start)``

TL;DR

``def row_mean(data, start, end):    ind = np.stack((start, end), axis=0)    ind += np.arange(data.shape[0]) * data.shape[1]    ind = ind.ravel(order='F')    if ind[-1] == data.size:        ind = ind[:-1]    return np.add.reduceat(data.ravel(), ind)[::2] / np.subtract(end, start)``

Timings

Using the exact same arrays shown in @Divakar's answer, we get the following results (specific to my machine of course):

``%timeit einsum_mean(data, start, end)261 ms ± 2.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)%timeit broadcasting_mean(data, start, end)405 ms ± 1.64 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)%timeit ragged_mean(data, start, end)520 ms ± 3.68 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)%timeit row_mean(data, start, end)45.6 ms ± 708 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)``

Somewhat surprisingly, this method runs 5-10x faster than all the others, despite doing lots of extra work by adding up all the numbers between the regions of interest. The reason is probably that it has extremely low overhead: the arrays of indices are small, and it only makes a single pass over a 1D array.

## Finding the sum of column of all the rows in a matrix without using numpy

You should use another for loop for `j` and `reinitialize` the result when a new column started to be processed.

``for j in range(col):   result = 0   for i in range(row):       result += mat1[i][j]   print(result)``

Can I insert the code in a function? After I got the first column's answer, I can increment j outside the loop and then call the
function? I think it is known as `recursion`.

Yes, you can do this with recursion.

``matrix = [[5, -1], [19, 8]]row = 2column = 2def getResult(j, matrix, result):   if j >= column:      return result   s = sum([matrix[i][j] for i in range(row)])   result.append(s)   return getResult(j + 1, matrix, result)result = getResult(0, matrix, [])``

Output

``> result[24, 7]``

## Python: general sum over numpy rows

According to numpy.sum documentation, you cannot specify `axis=1` for vectors as you would get a numpy `AxisError` saying `axis 1 is out of bounds for array of dimension 1`.

A possible workaround could be, for example, writing a dedicated function that checks the size before performing the sum. Please find below a possible implementation:

``import numpy as npM = np.array([[1, 4],             [2, 3]])v = np.array([1, 4])def sum_over_columns(input_arr):    if len(input_arr.shape) > 1:        return input_arr.sum(axis=1)    return input_arr.sum()print(sum_over_columns(M))print(sum_over_columns(v))``

In a more pythonic way (not necessarily more readable):

``def oneliner_sum(input_arr):    return input_arr.sum(axis=(1 if len(input_arr.shape) > 1 else None))``

## Sum Rows of 2D np array with List of Indices

I ended up coming up with my own solution when I realized I could sort my list without affecting my desired output. I used np.unique to determine the first indices of each element in the sorted list and then summed the rows between those indices. See below.

``elements, indices = np.unique(data, return_counts=True)row_summing = np.append([0], np.cumsum(indices))[:-1] #[0, index1, index2,...]output = np.add.reduceat(matrix, row_summing, axis=0)``