矩阵的相关系数

时间:2013-03-22 19:47:33

标签: c++ visual-c++

有人可以帮助我,因为我在这段代码上遇到错误,我不知道哪里出错了。我得到以下内容: 错误C2059:语法错误:'return', 错误C2334:'{'之前的意外标记;跳过明显的功能体, 错误C2039:'rtn':不是'corr_coefficient'的成员 看'corr_coefficient'的声明 谢谢。

using namespace std;

class corr_coefficient
{
    matrix x, y;
    double sum, C, corr[5];
    int j;  
public:
    double calc_mu(matrix x, int j=0);
    double calc_covariance(matrix x, matrix y, int j); 
    double calc_correlation();
    double StandardDeviation(matrix a, int j=0);
    double return(int i){return corr[i];
    };
    corr_coefficient(matrix x, matrix y);
    };

double corr_coefficient::calc_mu(matrix x, int j)
{
    sum=0;
    for(int i=0; i<C; i++)
    {
        sum = sum+x(i,j);
    }
    return sum/C;
}

double corr_coefficient::calc_covariance(matrix x, matrix y, int j)
{
    double mux=calc_mu(x,0); 
    double muy=calc_mu(y,j); 
    sum=0;
    for (int i=0; i<C; i++)
    {
        sum=sum+(x(i,0)-mux)*(y(i,j)-muy);
    }
    return sum/(C-1); 
}
double corr_coefficient::StandardDeviation(matrix a, int j)

{
    double mua=calc_mu(a,j); 

    sum=0;
    for (int i=0; i<C; i++)
    {
        sum=sum+((a(i,j))-mua)*((a(i,j))-mua);
    }
    return sqrt(sum/(C-1));
}
corr_coefficient::corr_coefficient(matrix x, matrix y)
{
    C=35;
    for (int j=0; j<5; j++)
    {
        corr[j]=calc_covariance(x, y, j)/(StandardDeviation(x, 0)*StandardDeviation(y, j));
    }
}

1 个答案:

答案 0 :(得分:3)

double return(int i){return corr[i];
};

return是一个关键字。您不能拥有名为return的函数。只是称它为不同的东西。或许operator[]适合在这里重载?

double operator[](int i){ return corr[i]; }