C++ - Vector 计算 均值(mean) 和 方差(variance)

时间:2014-04-28 10:44:42   收藏:0   阅读:587

Vector 计算 均值(mean) 和 方差(variance)


本文地址: http://blog.csdn.net/caroline_wendy/article/details/24623187


vector<>类型的数组, 计算均值和方差的最简方法.


代码:

	double sum = std::accumulate(std::begin(resultSet), std::end(resultSet), 0.0);
	double mean =  sum / resultSet.size(); //均值

	double accum  = 0.0;
	std::for_each (std::begin(resultSet), std::end(resultSet), [&](const double d) {
		accum  += (d-mean)*(d-mean);
	});

	double stdev = sqrt(accum/(resultSet.size()-1)); //方差


mamicode.com,码迷


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!