c++ - column vector with row means -- with std::accumulate? -



c++ - column vector with row means -- with std::accumulate? -

in effort lazy possible read in matrix

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

and seek utilize many stl goodies can.

one thing need next compute row means. in c-style programming be

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

in in c++, how compute mean of vector of integers using vector view , gsl_stats_mean? explained vector of numbers can compute vector mean in 1 line without calling size() operator @ every step:

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

is possible utilize these iterators on vector of vectors? intermediate form

vector<double> rowmeans( rows ); ( 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! using stl functions possible rid of [i] index well? (on top level it's matter of collecting row means).

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

although, personal style involve named lambda or function, because find more self-documenting:

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

c++ vector stl mean accumulate

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -