当在另一个范围中,将SelfAdjointeigensolver保存为成员的结果被重新引入

Result of SelfAdjointEigenSolver saved as member is reinitialized when in another scope

本文关键字:成员 结果 新引入 保存 SelfAdjointeigensolver 另一个 范围      更新时间:2023-10-16

我正在尝试在特征矩阵的块上运行PCA。输入矩阵中的观察值在列中。我想将特征向量保存为矩阵供以后使用。但是矩阵(m_pcacoefs("重新引导"当我在另一个课程中使用另一个范围时。

我很确定我缺少关于特征的工作方式!

class foo {
    public:
    using InputMatrixType = Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic>;
        void computePca(InputMatrixType & inputMatrix)
        {
            // m_pcaCoefs is a private member of dense matrix type
            size_t start = 1;
            auto r = inputMatrix.rows();
            auto c = inputMatrix.cols(); 
            Eigen::Block<InputMatrixType>  inputBlock 
                  = inputMatrix.block( start, 0 ,r-start , c   );
            // center the data
            m_pixelValueMeans = inputBlock.rowwise().mean();
            inputBlock.colwise() -= m_pixelValueMeans;
            // inputBlock is a d by n, where d is the number of observation
            InputMatrixType cov = inputBlock * inputBlock.adjoint();
            cov = cov / (c - 1);
            Eigen::SelfAdjointEigenSolver<InputMatrixType> eig(cov);
            InputMatrixType m_pcaCoefs = eig.eigenvectors();
            // here m_pcaCoefs looks fine
            std::cout << m_pcaCoefs.size() << std::endl; // output: 9  
        }
        void print()
        {
            std::cout << m_pcaCoefs.size() << std::endl; // output: 0
        }
    protected:
       InputMatrixType m_pcaCoefs;
}

int main()
{
    foo test;
    test.computePca(someMatrix); // outputs 9
    test.print() // output 0
}

有什么线索如何将特征向量复制到m_pcacoefs?

InputMatrixType m_pcaCoefs = eig.eigenvectors();

这不是您的班级成员的想法。

您应该只使用:

m_pcaCoefs = eig.eigenvectors(); // use member m_pcaCoefs
相关文章: