1

I have an Eigen matrix with a vector as the scalar type, defined as the following

Eigen::Matrix<Eigen::VectorXd, Eigen::Dynamic, Eigen::Dynamic> res = Eigen::Matrix<Eigen::VectorXd, Eigen::Dynamic, Eigen::Dynamic>::
        Constant(10,
            10,
            Eigen::VectorXd::Zero(3));

I can access and assign values as the following

res(0,0)(0) = 1;

I am trying to assign a matrix to each third index as the following

res(all_rows,all_cols)(0) = matrix

The following is my code

int main()
{
    Eigen::Matrix<Eigen::VectorXd, Eigen::Dynamic, Eigen::Dynamic> res = Eigen::Matrix<Eigen::VectorXd, Eigen::Dynamic, Eigen::Dynamic>::
        Constant(10,
            10,
            Eigen::VectorXd::Zero(3));

    Eigen::MatrixXf x = Eigen::MatrixXf::Ones(10, 1)*2;
    Eigen::MatrixXf y = Eigen::MatrixXf::Ones(10, 1);
    Eigen::MatrixXf product = Eigen::MatrixXf::Zero(10, 10);
    MatrixXd m(2, 2);
    res(0, 0)(0) = 1;
    m(0, 0) = 3;
    m(1, 0) = 2.5;

    m(0, 1) = -1;
    m(1, 1) = m(1, 0) + m(0, 1);
    product = x*y.transpose();
    res.block(0,0,10,10)(0) = x*y.transpose();
}

But when I try to compile the code, This line is giving the error

res.block(0,0,10,10)(0) = x*y.transpose();

I am getting the following following error.

Error   C2338   THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS

Is there a way to overcome this, or should I change my approach?

2
  • 1
    You are assigning the outer product x*y^T matrix to the first element of the 10x10 block which is an Eigen::VectorXd. Looks like you are looking for tensor types.
    – cassinaj
    Mar 22, 2017 at 15:28
  • 1
    You have to define several scalar operations, if you use Eigen::VectorXd as scalar. A better option is either using a Eigen::Tensor of rank 3, or a flattened matrix of size (3, width*height) with usual scalar (double in your case)
    – iNFINITEi
    Mar 23, 2017 at 16:29

1 Answer 1

5

You are trying to use Eigen::VectorXd as the scalar type. So you should make sure you have extended Eigen::NumTraits as explained in https://eigen.tuxfamily.org/dox-devel/TopicCustomizing_CustomScalar.html

It looks like you are trying to recreate a multi-channel array or image. So you won't gain a lot by the above method. A better idea is to use either a Eigen::Tensor of rank 3, or a flattened matrix of size (3, width*height). Then you get all the Eigen benefits (vectorized scalar operations, no weird access pattern like (row, col)(channel), etc )

Eigen::Tensor<double, 3> rgb_image(height, width, 3);
Eigen::MatrixXd rgb_image(3, width*height); // Each column is a pixel color
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.