Search code examples
c++eigeneigen3

Eigen equivalent of Matlab's sum(A)?


How can I get the row vector, each value of which is the sum of each column of a 2D Array in Eigen?

I want the equivalent of the Matlab sum function, i.e.

>> x = [1,2,3;4,5,6]

x =

     1.0000e+000     2.0000e+000     3.0000e+000
     4.0000e+000     5.0000e+000     6.0000e+000

>> sum (x)

ans =

     5.0000e+000     7.0000e+000     9.0000e+000

can I use colwise to do this somehow?


Solution

  • You can use m.colwise().sum().

    See at Combining partial reductions with other operations section here.