有人請告訴我如何解決這個問題。選擇並分析一個數組中的點的窗口
我有一個函數對數組中包含的一組點進行簡單的迴歸分析。 我有一個數組(pval),其中包含我想要執行迴歸分析的所有數據。 這就是我想要實現這個的方法。
我得到了數組中前7個元素的平均值。這就是我在程序中所謂的'ref_avg'。
我想對數組中的每五個元素執行一次迴歸分析,將該數組的第一個元素作爲'ref_avg'。在迴歸分析的每一步中,我將在數組中得6分。
例如 對於第一步,下面計算的ref_avg是70.78。因此,在簡單迴歸的第一步驟將包含這些點
第一= {70.78,76.26,69.17,68.68,71.49,73.08},
第二步將包含ref_avg作爲第一元件和其它元件從第二元件原始數組中開始
第二= {70.78,69.17,68.68,71.49,73.08,72.99},
第三= {70.78,68.68,71.49,73.08,72.99,70.36},
4th = {70.78,71.49,73.08,72.99,70.36,57.82}等等直到最後。
迴歸函數如下所示。
我不理解爲什麼「演算」數組的第一個3種元素對迴歸的第一步驟中值0.00,2種元素的第二步驟,在第三1個元件。 也是迴歸函數的最後一步打印3次。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
float pval[]={76.26,69.17,68.68,71.49,73.08,72.99,70.36,57.82,58.98,69.71,70.43,77.53,80.77,70.30,70.5,70.79,75.58,76.88,80.20,77.69,80.80,70.5,85.27,75.25};
int count,Nhour;
const int MAX_HOUR = 24;
float *calcul=NULL;
float *tab_time =NULL;
float ref_avg;
int size_hour=7;
float sum=0;
int length = Nhour+1;
float m;
float b;
calcul=(float*)calloc(MAX_HOUR,sizeof(calcul));
if (calcul==NULL)
{
printf(" error in buffer\n");
exit(EXIT_FAILURE);
}
tab_time= calloc(MAX_HOUR,sizeof(float));
/* Get the average of the first seven elements */
int i;
for (i=0;i<size_hour;i++)
{
sum += pval[i];
}
ref_avg = sum/size_hour;
count=0;
/* perform the regression analysis on 5 hours increment */
while(count<=MAX_HOUR)
{
++count;
Nhour=5;
int pass = -(Nhour-1);
int i=0;
for(i=0;i<Nhour+1;i++)
{
if(count<MAX_HOUR)
{
calcul[0]=ref_avg;
calcul[i] =pval[count+pass];
pass++;
}
printf("calc=%.2f\n",calcul[i]); // For debug only
tab_time[i]=i+1;
if(i==Nhour)
{
linear_regression(tab_time, calcul, length, &m, &b);
printf("Slope= %.2f\n", m);
}
}
}
free(calcul);
calcul=NULL;
free(tab_time);
tab_time=NULL;
return 0;
}
/* end of the main function */
/* This function is used to calculate the linear
regression as it was called above in the main function.
It compiles and runs very well, was just included for the
compilation and execution of the main function above where I have a problem. */
int linear_regression(const float *x, const float *y, const int n, float *beta1, float *beta0)
{
float sumx = 0,
sumy = 0,
sumx2 = 0,
sumxy = 0;
int i;
if (n <= 1) {
*beta1 = 0;
*beta0= 0;
printf("Not enough data for regression \n");
}
else
{
float variance;
for (i = 0; i < n; i++)
{
sumx += x[i];
sumy += y[i];
sumx2 += (x[i] * x[i]);
sumxy += (x[i] * y[i]);
}
variance = (sumx2 - ((sumx * sumx)/n));
if (variance != 0) {
*beta1 = (sumxy - ((sumx * sumy)/n))/variance;
*beta0 = (sumy - ((*beta1) * sumx))/n;
}
else
{
*beta1 = 0;
*beta0 = 0;
}
}
return 0;
}
你在'而(計數{')發佈的代碼,甚至不編譯(語法錯誤巨大的塊。嘗試減少你的程序中表現出的問題,並編譯後的代碼更小的程序。當你處於這種狀態時,請正確地縮進你的程序,這樣人們可能會想要閱讀它。 – Gilles 2010-08-24 21:12:34
@ Gilles:感謝您的評論。我認爲在粘貼代碼時我是一種失敗。我糾正了現在可以編譯的錯誤。 – chriscol 2010-08-25 08:51:34