简体   繁体   中英

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. 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. 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. 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 . 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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