简体   繁体   中英

How to assing a vector to a matrix in eigen

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)

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++) )

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