Summing across rows/columns of a matrix

169 views
Skip to first unread message

Daniel Salvadori

unread,
Aug 29, 2018, 11:38:08 AM8/29/18
to gonum-dev
Is there an API for summing across rows or columns of a matrix? I couldn't find it.
If there's no API yet, would the community find beneficial for me to implement a mat.SumRows(a Matrix) and a mat.SumCols(a Matrix) ?

Brendan Tracey

unread,
Aug 30, 2018, 1:21:14 AM8/30/18
to gonum-dev
No, there's no such way.

The problem is that this kind of thing doesn't compose. There are many kinds of operations one might want to do (sum, max, mean, std, etc.). Instead, use mat.Row and floats (or appropriate changes for other functions).

sums := make([]float64, c)
col := make([]float64, r)
for i := 0; i < r ; i++{
    mat.Col(col, a)
    sums[i] = floats.Sum(col)
}

Note that if you have a Dense matrix, you can also use dense.RawRowView to speed things up


Reply all
Reply to author
Forward
0 new messages