[英]Error in CoeffRef of Eigen sparse matrix with elements type of a vector
I define a sparse matrix as Eigen::SparseMatrix< Eigen::Matrix<float, 3, 1> >
, which means each element of the matrix is a 3x1 vector. 我将稀疏矩阵定义为Eigen::SparseMatrix< Eigen::Matrix<float, 3, 1> >
,这意味着矩阵的每个元素都是3x1向量。 However, when I am calling the function CoeffRef in order to assign a vector into the elements, I got the following error in SparseMatrix.h: 但是,当我调用函数CoeffRef以将向量分配给元素时,我在SparseMatrix.h中收到以下错误:
no operator "=" matches these operands.
and the error comes from the function insert
, while it assigns an int to an Eigen::Matrix< float, 3, 1>
, which is m_data.value(p) = 0
(considering that m_data.value(p)
is a vector 3x1 and 0 is an int). 并且错误来自函数insert
,而它将一个int赋给一个Eigen::Matrix< float, 3, 1>
,这是m_data.value(p) = 0
(考虑到m_data.value(p)
是一个向量3x1和0是一个int)。
It seems that in this line of code (line 1235 of SparseMatrix.h), they did not take into account the template type of matrix for the comparison. 似乎在这行代码(SparseMatrix.h的第1235行)中,他们没有考虑用于比较的矩阵的模板类型。
I was wondering if you would have any sort of ideas to solve this error? 我想知道你是否有任何想法来解决这个错误?
typedef Eigen::Matrix< float, 3, 1> Vec3f;
Eigen::SparseMatrix< Vec3f > lA( m, n);
lA.reserve( Eigen::VectorXi::Constant(m, 4) );
for( unsigned int i = 0; i < m; i++)
{
Vec3f lVec( 0.0, 0.0, 1.0);
lA.coeffRef(i, i) = lVec; // got the error here!
}
This is because coeffRef
tries to initialize the newly created element to 0, but 0 cannot be assigned to a Vector3f
. 这是因为coeffRef
尝试将新创建的元素初始化为0,但不能将0分配给Vector3f
。 So the solution is to use an Array3f
instead: 所以解决方案是使用Array3f
代替:
typedef Eigen::Array<float, 3, 1> Vec3f;
Of course, beware that operator* behave differently on Array
than on vector and matrices. 当然,请注意运算符*在Array
上的行为与向量和矩阵的行为不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.