我正在尝试计算(在VBA Excel中)超过65536个元素的数组的Average和StDev。像这样:
Mitja = worksheetfunction.Average(array()) DesvTip = worksheetfunction.StDev(array())
虽然阵列的尺寸小于65536但没有问题,但是当它更大时会给我一个错误!
我知道这个VBA函数不能使用超过65536个数据,所以如何在VBA中获取这些参数?
Apreciate您的意见。非常感谢! :))
答案 0 :(得分:1)
您可以计算平均值和标准差,而无需存储所有值。只需保持总和,平方和和点数。你可以拥有与整数个点数一样多的点。
以下是我在Java中的表现。随意小儿床。
package statistics;
/**
* Statistics
* @author Michael
* @link http://stackoverflow.com/questions/11978667/online-algorithm-for-calculating-standrd-deviation/11978689#11978689
* @link http://mathworld.wolfram.com/Variance.html
* @since 8/15/12 7:34 PM
*/
public class Statistics {
private int n;
private double sum;
private double sumsq;
public void reset() {
this.n = 0;
this.sum = 0.0;
this.sumsq = 0.0;
}
public synchronized void addValue(double x) {
++this.n;
this.sum += x;
this.sumsq += x*x;
}
public synchronized double calculateMean() {
double mean = 0.0;
if (this.n > 0) {
mean = this.sum/this.n;
}
return mean;
}
public synchronized double calculateVariance() {
double variance = 0.0;
if (this.n > 0) {
variance = Math.sqrt(this.sumsq-this.sum*this.sum/this.n)/this.n;
}
return variance;
}
public synchronized double calculateStandardDeviation() {
double deviation = 0.0;
if (this.n > 1) {
deviation = Math.sqrt((this.sumsq-this.sum*this.sum/this.n)/(this.n-1));
}
return deviation;
}
}
答案 1 :(得分:0)
如果数据存储在数组x(1 to N, 1 to 1)
中,则使用以下算法,其中N
是数据点的数量
sum = 0# : sumsq = 0#
for i=1 to N
sum = sum + x(i,1)
sumsq = sumsq + x(i,1)^2
next i
average = sum/N
stddev = Sqr( sumsq/N^2 - sum^2/N^3 )
:注意: 要填充数组,请使用符号
Dim r as Range, x() as Variant
Set r = Range("A1").Resize(N,1)
x = r.Value
答案 2 :(得分:0)
感谢您的评论。最后我们做了类似的事情。我希望它对有同样问题的人有用。我们的代码:
sum = 0
sumq = 0
For i = 0 To ((2 * N) - 1)
sum = sum + h_normal(i)
Next i
media = sum / (2 * N)
For j = 0 To ((2 * N) - 1)
sumsq = sumsq + (h_normal(j) - media) ^ 2
Next j
desviaci(h - 1) = Math.Sqr(sumsq / ((2 * N) - 1))