column vector with row means — with std::accumulate?

泪湿孤枕 提交于 2019-12-21 17:22:21

问题


In an effort to be as lazy as possible I read in a matrix as

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

and try to use as many STL goodies as I can.

One thing I need to do next is to compute the row means. In C-style programming that would be

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 compute the mean of a vector of integers using a vector view and gsl_stats_mean? it is explained that for a vector of numbers you can compute a vector mean in one line without calling the size() operator at every step:

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

Is it possible to use these iterators over a vector of vectors? An intermediate form is

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 gone! but using STL functions is it possible to get rid of the [i] index as well? (on the top level it's just a matter of collecting the row means).


回答1:


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

Although, my personal style would involve a named lambda or function, because I would find that more self-documenting:

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);



回答2:


Using boost::numeric::ublas however (if it's an option):

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


来源:https://stackoverflow.com/questions/14915602/column-vector-with-row-means-with-stdaccumulate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!