QT/C++/Eigen 自己实现的MATLAB中的std()函数,矩阵按照列求标准差

QT / C++ / Eigen 自己实现的MATLAB中的std()函数,矩阵按照列求标准差

//定义一个按列求标准差的函数,其中A为被求列标准差的矩阵,OutputMatrix为求出的列标准差
void Std(MatrixXd &A, MatrixXd &OutputMatrix)
{
    //按列求标准差
    int Rows = A.rows();
    int Cols = A.cols();
    //按列求平均值
    MatrixXd A_ColsMean(1,Cols);
    A_ColsMean = A.colwise().mean();
    //cout << "A_ColsMean:" << endl << A_ColsMean << endl;

    //矩阵中每一列的值减去其对应的列平均值
    MatrixXd A_std1(Rows,Cols);
    for(int i = 0;i < Rows; i++)
    {
        for(int j = 0;j < Cols; j++)
        {
            A_std1(i,j) = abs(pow((A(i,j) - A_ColsMean(0,j)),2));
        }
    }
    //cout << "A_std1:" << endl << A_std1 << endl;

    MatrixXd A_std2(1,Cols);
    A_std2 = A_std1.colwise().sum();
    //cout << "A_std2:" << endl << A_std2 << endl;

    MatrixXd A_std3(1,Cols);
    int n_std;
    if(Rows > Cols)
    {
        n_std = Rows - Cols - 1;
        for(int i = 0;i < Cols; i++)
        {
            A_std3(0,i) = A_std2(0,i) / (Cols + n_std);//
        }
    }
    else if(Rows < Cols)
    {
        n_std = Cols - Rows + 1;
        for(int i = 0;i < Cols; i++)
        {
            A_std3(0,i) = A_std2(0,i) / (Cols - n_std);//
        }
    }
    else if(Rows = Cols)
    {
        n_std = 1;
        for(int i = 0;i < Cols; i++)
        {
            A_std3(0,i) = A_std2(0,i) / (Cols - n_std);//
        }
    }
    //qDebug() << "n_std:" << n_std;

    MatrixXd A_std(1,Cols);
    for(int i = 0;i < Cols; i++)
    {
        A_std(0,i) = sqrt(A_std3(0,i));
    }
    //cout << "A_std:" << endl << A_std << endl;
    OutputMatrix = A_std;
}
``
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值