0
votes

I want to assign a vector to a matrix of Dynamic size. As in the following code:

    Eigen::Vector3f VectorCam; // Vector in camera frame
    Eigen::MatrixXf VectorsCam; // Matrix contains the vectors

    for (int i=0; i<=theta1.size(); i++)
    {
        std::cout << "I'm In theta1.size for loop" << std::endl;    
        VectorCam  << sin(theta1[i]), sin(theta2[i]), cos(theta1[i])*cos(theta2[i]);
        std::cout << "theta1.size is:" << theta1.size() << std::endl;
        std::cout << VectorCam << std::endl;
        VectorsCam.col(i) = VectorCam;      // Matrix of Camera Vectors
        std::cout << "Vectorscam" << VectorsCam << std::endl;
        
    } 

in the terminal I'm getting this (below #### )! Knowing that the for loop is still in the first run and it is supposed to run at least 2 times before exiting ! I thimk the problem is in VectorsCam.col(i)

######## I'm In theta1.size for loop theta1.size is:1 1 1 7.82347e-10 visual_servo_node: /usr/include/eigen3/Eigen/src/Core/Block.h:123: Eigen::Block<XprType, BlockRows, BlockCols, InnerPanel>::Block(XprType&, Eigen::Index) [with XprType = Eigen::Matrix<float, -1, -1>; int BlockRows = -1; int BlockCols = 1; bool InnerPanel = true; Eigen::Index = long int]: Assertion `(i>=0) && ( ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i<xpr.rows()) ||((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && i<xpr.cols()))' failed. Aborted (core dumped)

1

1 Answers

0
votes

Before calling VectorsCam.col(i) you must make sure, VectorsCam has at least i+1 columns. And when assigning VectorsCam.col(i) = VectorCam; the number of rows in VectorsCam must match the number of rows in VectorCam.

Before the loop write

Eigen::MatrixXf VectorsCam(3, theta1.size()); // Matrix contains the vectors

(and I assume the loop was meant to be for (int i=0; i<theta1.size(); i++))