当在另一个范围中,将SelfAdjointeigensolver保存为成员的结果被重新引入
Result of SelfAdjointEigenSolver saved as member is reinitialized when in another scope
我正在尝试在特征矩阵的块上运行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
相关文章:
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 当在另一个范围中,将SelfAdjointeigensolver保存为成员的结果被重新引入
- 如何修改 std::generate 的绑定成员函数的结果
- 是否可以将类成员变量设置为类函数的结果?
- 多态性:成员访问和吸气器给出不同的结果
- 为什么 G++ 不警告 const 成员的未使用结果?
- 只有当声明一个对象时,内存空间才会分配给类的数据成员,那么为什么 Sizeof(class_name) 给我结果
- 向量的最后一个成员在 Rcpp 中没有给出正确的结果
- 如何在未评估的上下文中将成员函数的结果类型应用于类成员
- std::具有自定义比较函数结果的排序函数错误:必须调用对非静态成员函数的引用
- 将cv::Mat A的每个成员与cv::Mat B合并,以创建一个cv::Mat C,结果为[A(x,y),B(x,y)
- 返回成员变量时,为什么函数内外得到的结果不同
- 将重载+成员函数的结果发送到重载<<导致编译错误
- 使用函数调用结果在C++03中初始化多个成员
- C++成员函数结果缓存实现
- 将 std::forward_as_tuple() 结果传递给可能从该对象的 rvalue-reference 成员移动的多个函数?
- 重载成员函数访问元组和递归累加结果失败的原因
- 如何用函数的结果初始化静态成员数组
- 是否有可能将一个动作附加到boost::spirit::规则解析器,该解析器将解析结果分配给(尚未)未知实例的成员
- 如何仅使用元素成员函数和STL在容器中积累来自元素成员函数的结果