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

左心房为你撑大大i 提交于 2019-12-04 08:36:29
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);

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