@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