@Charles。啊,不。它只是一个公式。误读规范。
如果你想计算推到C++和揭露它作为一个XLL,这里是你会怎么做:
#include <algorithm>
#include <numeric>
#include "xll/xll.h"
using namespace xll;
typedef traits<XLOPER12>::xword xword;
static AddIn12 xai_windowed_average(
L"?xll_windowed_average", XLL_FP12 XLL_FP12 XLL_FP12 XLL_DOUBLE12,
L"WINDOWED.AVERAGE", L"Time, Value, Window"
);
_FP12* WINAPI
xll_windowed_average(_FP12* pt, _FP12* pv, double dt)
{
#pragma XLLEXPORT
static xll::FP12 a(size(*pt), 1);
double* bt0 = &pt->array[0];
double* bv0 = &pv->array[0];
double* bt = std::lower_bound(begin(*pt), end(*pt), *bt0 - dt);
double* et = std::lower_bound(begin(*pt), end(*pt), *bt0 + dt);
for (xword i = 0; i < size(*pt); ++i) {
a[i] = (bt == et) ? 0 : std::accumulate(bv0 + (bt - bt0), bv0 + (et - bt0), 0)/(et - bt);
// update the window
bt = std::lower_bound(bt, end(*pt), pt->array[i] - dt);
et = std::lower_bound(bt, end(*pt), pt->array[i] + dt);
}
return a.get();
}
对不起,我不记得Excel的公式非常好:但是该公式在单元格基础上还是共同地在单元格A和单元格B之间生成平均值? – 2012-07-08 18:47:29
@ Battle_707该公式是对列B中具有+/- 1000(A列)时间戳的值按行进行平均。每一行都有它自己的值,这些值在其任一侧的范围内。 – 2012-07-08 18:54:27
我很抱歉,但我还是不太明白。如果您用于平均的唯一数字是B行,您如何才能做出每行平均值?如果我猜测你想要做的是从某个时间戳中选择所有具有+/- 1000值的行,并在这些行之间取平均值。在MySQL中,通过使用AVG函数和一些条件选择来完成。例如:'SELECT AGV(B)FROM t1 WHERE a''t-1000'and a''$ t + 1000'',其中$ t是时间戳。如果您正确索引此表(t1),则此查询应该非常快。 – 2012-07-08 19:06:25