简体   繁体   English

如何将1x1 Eigen :: C ++稀疏矩阵转换为整数值?

[英]How do I convert a 1x1 Eigen::C++ sparse matrix to an integer value?

This is what I am trying to do, (I have removed the technical part and simplified the problem) 这就是我想要做的,(我已经删除了技术部分并简化了问题)

I have predefined these matrices, sequence, u1, u2. 我已经预定义了这些矩阵,序列,u1,u2。 They have to be matrices in sparse form. 它们必须是稀疏形式的矩阵。

MatrixXi sequence(1,5)
SparseMatrix<int> u1(5,1)
SparseMatrix<int> u2(5,1)

Now, I would like to perform sequence(0)=u1'*u2. 现在,我想执行sequence(0)= u1'* u2。 This is my code. 这是我的代码。

sequence(i)=(((u1.transpose())*u2).coeffRef(0,0))

but I get this error 但是我得到这个错误

[Error] 'const Type' has no member named 'coeffRef'. 

I know it is because a 1x1 sparse matrix is not equivalent to an integer. 我知道这是因为1x1稀疏矩阵不等于整数。 How should I approach this? 我应该如何处理? Somehow, I have to convert it to an integer. 不知何故,我必须将其转换为整数。

The issue is that (u1.transpose() * u2) is not actually (by default) a matrix object but rather a Eigen::SparseSparseProduct . 问题是(u1.transpose() * u2)实际上(默认情况下)不是矩阵对象,而是Eigen::SparseSparseProduct You can get around this by either assigning it to a temporary variable: 您可以通过将其分配给一个临时变量来解决此问题:

u3 = (u1.transpose() * u2);
sequence(0,0) = u3.coeffRef(0,0);

or by casting it: 或通过强制转换:

sequence(0,0) = (SparseMatrix<int>(u1.transpose() * u2)).coeff(0,0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM