我想计算一组循环数据的平均值.例如,我可能会从阅读指南中获得几个样本.问题当然是如何处理环绕.相同的算法可能对时钟表有用.
实际问题更复杂 - 统计在球体上或在"包裹"的代数空间中意味着什么,例如添加剂组mod n.答案可能不是唯一的,例如359度和1度的平均值可能是0度或180度,但统计上0看起来更好.
这对我来说是一个真正的编程问题,我试图让它看起来不像是一个数学问题.
所以我试图弄清楚如何计算颜色由HSL值表示的多个对象的平均色调.值得庆幸的是,我偶然发现了这个Stack Overflow帖子,并开始实现顶部答案中提供的算法(我在C++中工作).
不幸的是,我的实现似乎不起作用.在这里,它是完整的; 请注意,虽然我写了"Hue",但我按照初始实现使用角度(以度为单位)(从0-360角度切换到0-256色调,一旦我知道我的代码有效,就不应该很难).
#include <iostream>
#include <vector>
#include <cmath>
#define PI (4*atan(1))
int main()
{
///
/// Calculations adapted from this source:
/// https://stackoverflow.com/questions/8169654/how-to-calculate-mean-and-standard-deviation-for-hue-values-from-0-to-360
std::vector<double> Hues = {355, 5, 5, 5, 5};
//These will be used to store the sum of the angles
double X = 0.0;
double Y = 0.0;
//Loop through all H values
for (int hue = 0; hue < Hues.size(); ++hue)
{
//Add the X and Y values to the sum X and …
Run Code Online (Sandbox Code Playgroud)