I have a matrix of matrices in python, a list of lists inside the main matrix I have 36 matrices, I need the sum of all 36 matrices like the sum of rows of the matrix in one row, here I need a sum of all 36 matrices in one matrix. any ideas, please?

python_sum of matrices

0

Entering edit mode

4.0 years ago

lucas_
▴
20

I have a matrix of matrices in python, a list of lists inside the main matrix I have 36 matrices, I need the sum of all 36 matrices like the sum of rows of the matrix in one row, here I need a sum of all 36 matrices in one matrix. any ideas, please?

1

Entering edit mode

4.0 years ago

Joe
20k

I'll have a stab at this, but it needs more clarification from OP.

Here is a 'base' python way of dealing with the problem purely as lists.

Assuming the following dummy 'matrix of matrices':

```
>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> M = [matrix, matrix, matrix]
>>> print(M)
# [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]]
```

Define a function capable of summing a single matrix (a 2D array):

```
>>> def mat_sum(input):
return sum(map(sum, input))
```

Then,

```
>>> mat_sum(matrix)
45
```

To then deal with your higher dimensional matrix, you could do something like:

```
# Sums of each 'sub matrix'
>>> [mat_sum(i) for i in M]
[45, 45, 45]
# Sum of whole matrix
>>> sum([mat_sum(i) for i in M])
135
```

A better way would probably be to convert the matrices to `numpy`

objects which has methods for handling this already:

```
import numpy as np
>>> np_matrix = np.matrix(matrix)
>>> np_matrix
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Unfortunately, numpy matrices must be 2D, so we have to assign the top level matrix to an array instead
>>> np_M = np.array(M)
>>> np_M
array([[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]],
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]],
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]])
# However the sum function works just fine either way...
>>> np.sum(np_matrix)
45
>>> np.sum(np_M)
135
```

Similar Posts

Loading Similar Posts

Traffic: 1638 users visited in the last hour

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Hello lucas_!

We believe that this post does not fit the main topic of this site.

Not a bioinformatics question!

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

## JH Edited Content:

Reopened the thread since it got an answer prior to being locked.

I am sorry to be late for answering, the idea is i need to extract from sequence profile ,windows of 16 residues for each position the matrix rows look like that

every list [] includes float values

i need the sum of all matrices inside the big matrix in one matrix that is mean one matrix

`[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]`

See my answer in that case, I think it should work for your needs.

Can you clarify or add some example data?

It’s not clear to me if you want the sum of some particular row, or the sum of all 36 ‘internal’ matrices, or the sum of everything etc.?