Column vector with string tools - with std :: accumulate?

Trying to be as lazy as possible, I read in the matrix how

vector< vector<double> > data ( rows, vector<double> ( columns ) );

and try to use as many STL treats as possible.

One thing I need to do next is to compute the string tool. In C style programming, which will

vector<double> rowmeans( data.size() );
for ( int i=0; i<data.size(); i++ )
    for ( int j=0; j<data[i].size(); j++ )
        rowmeans[i] += data[i][j]/data[i].size();

In In C ++, how to calculate the average of a vector of integers using a vector representation and gsl_stats_mean? , it is explained that for a vector of numbers you can calculate a vector value in one line without calling the size () operator at each step:

double mean = std::accumulate(stl_v.begin(), stl_v.end(), 0.0) / stl_v.size();

Can these iterators be used over a vector of vectors? Intermediate form

vector<double> rowmeans( rows );
    for ( int i=0; i<data.size(); i++ )
        rowmeans[i] = std::accumulate(data[i].begin(), data[i].end(), 0.0) / data[i].size();

already 1 line is gone! but using STL functions, is it possible to get rid of index [i]? (at the top level, it's just a matter of gathering strings).

+5
2
std::transform(data.begin(), data.end(), rowmeans.begin(),
    [](std::vector<double> const& d) {
        return std::accumulate(d.begin(), d.end(), 0.0) / d.size();
    });

, , :

auto Mean = [](std::vector<double> const& d) { return std::accumulate(d.begin(), d.end(), 0.0) / d.size(); };
std::transform(data.begin(), data.end(), rowmeans.begin(), Mean);
+11

boost::numeric::ublas ( ):

matrix<double> m(rows,columns);
double mean = accumulate(m.data().begin(),m.data().end(),0,std::max<double>) / (rows * columns);
+1

All Articles