返回信息流写了一个计算协方差的程序,计算原理参看
http://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm
然后下面是我写的程序,在DEV下运行
#include <iostream>
using namespace std;
int main()
{
double hello[16][4] = {56.5, -25.5, 39.25, 73.369,
36.5, -6, 34.75, 50.752,
13.75, 16, 28.25, 35.258,
4.25, 22.25, 27.5, 35.628,
15.25, 8.75, 31.75, 36.293,
35.75, -18, 36.5, 54.169,
54, -41.5, 41.25, 79.623,
56.75, -48, 42.25, 85.496,
44, -35, 39.25, 68.568,
19.75, -6.25, 34.5, 40.241,
-0.25, 18.75, 29.5, 34.955,
-6, 24, 28.75, 37.928,
6.75, 5.25, 31.75, 32.881,
29.5, -27.25, 36.25, 54.101,
47.5, -53.25, 41.5, 82.547,
51, -58.5, 42.75, 88.605 };
double temp[4];
double ans[4][4];
double cov[4][4];
for (int x = 0; x<16; x++) //可以改
{
for (int y =0; y<4; y++)
{
cout << hello[x][y]<<" ";
}
cout<<endl;
}
for (int j=0; j<4; j++){
double sum1 = 0;
for (int i=0; i<16; i++){ //可以改
sum1 = sum1 + hello[i][j];
}
temp[j] = sum1/16; //可以改
}
cout<<temp[0]<<" "<<temp[1]<<" "<<temp[2]<<" "<<temp[3];
cout<<endl;
double sum2 = 0;
for(int i=0; i<16; i++){ //可以改
for (int a=0; a<4; a++){
for(int b=0; b<4; b++){
ans[a][b] = ans[a][b] + (hello[i][a]-temp[a])*(hello[i][b]-temp[b]);
}
}
}
for (int c=0; c <4; c++){
for (int d=0; d<4; d++){
cov[c][d] = ans[c][d]/15; //可以改
cout<<cov[c][d]<<" ";
}
cout<<endl;
}
system("pause");
return 0;
}
计算的结果
466.663 -559.761 108.476 415.408
7.90371e+290 7.90367e+290 7.90322e+29
108.476 4.33393e+267 5.04767e+290 4.3
415.408 -558.057 105.382 4.2168e+290
其中出现很奇怪的 “e+290”,与matlab计算结果对比,发现除了带有“e+”的项不对外,其他的项一样
不明白为什么是这样
还有,如果将程序的hello[16][4]改为hello[12][4],(及消去几行数)
同时将注释有“//可以改”的那几行的数字16 16 16 16 15 改为12 12 12 12 11程序运行良好,各位知道是什么原因么?有何解决方法不?
这是一条镜像帖。来源:北邮人论坛 / cpp / #42402同步于 2010/8/14
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
计算的结果出现了"e+ 290"这种错误
weiwei
2010/8/14镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复