# sum of elements by row or column in a matrix in Sage

in Numpy, we can use sum(M,axis=0) or sum(M,axis=1)

what is the equivalent instruction syntax with sage ?

sum of elements by row or column in a matrix in Sage

in Numpy, we can use sum(M,axis=0) or sum(M,axis=1)

what is the equivalent instruction syntax with sage ?

add a comment

2

For summing rows:

```
sage: M = random_matrix(ZZ,3,5)
sage: M
[ -2 1 -10 -1 -1]
[ -2 1 -1 1 -1]
[ -3 5 -2 1 2]
sage: sum(M)
(-7, 7, -13, 1, 0)
```

For the columns:

```
sage: sum(M.columns())
(-13, -2, 3)
```

or

```
sage: sum(M.T)
(-13, -2, 3)
```

Asked: **
2016-06-26 03:19:07 -0600
**

Seen: **790 times**

Last updated: **Jun 26 '16**

How to calculate sine of a matrix

How to build a matrix thought of as an array of smaller matrices?

Random positive definite matrix

Map a function to each element of a matrix?

Get a matrix to display answers as decimals/floats, not fraction?

How to get the coefficient of $x^n$ in symbolic expression of matrix

Why can't I find exponential of a "complex256" matrix?

Using sage to check if a vector is in a matrix's null space

How can I plot matrices from Database into different sage cells?

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.