【问题标题】:How to assing a vector to a matrix in eigen如何将向量分配给特征中的矩阵
【发布时间】:2020-06-27 08:25:25
【问题描述】:

我想将一个向量分配给一个动态大小的矩阵。如以下代码:

    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;
        
    } 

在终端中我得到了这个(低于 #### )!知道 for 循环仍在第一次运行中,它应该在退出之前运行至少 2 次!我认为问题出在 VectorsCam.col(i)

######## 我在 theta1.size for 循环中 theta1.size 是:1 1 1 7.82347e-10 visual_servo_node: /usr/include/eigen3/Eigen/src/Core/Block.h:123: Eigen::Block::Block(XprType&, Eigen::Index) [with XprType = Eigen ::矩阵; int BlockRows = -1; int BlockCols = 1;布尔内面板=真; Eigen::Index = long int]: 断言 `(i>=0) && ( ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i

【问题讨论】:

    标签: c++ matrix eigen3


    【解决方案1】:

    在调用VectorsCam.col(i) 之前,您必须确保VectorsCam 至少有i+1 列。并且在分配VectorsCam.col(i) = VectorCam; 时,VectorsCam 中的行数必须与VectorCam 中的行数匹配。

    循环前写

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

    (我认为循环应该是for (int i=0; i&lt;theta1.size(); i++)

    【讨论】:

    • 我想学习的不仅仅是让代码工作,那么,为什么单独使用“Eigen::MatrixXf VectorsCam”仍然会给我同样的错误?
    • 构造一个不带参数的MatrixXf 将创建一个0x0 矩阵。而且您无法访问矩阵大小之外的列(或任何元素)(不会发生“自动附加”)。
    猜你喜欢
    • 2018-10-12
    • 2019-08-16
    • 2019-04-27
    • 2016-07-24
    • 2012-08-08
    • 2022-01-05
    • 1970-01-01
    • 2017-09-27
    • 2021-12-03
    相关资源
    最近更新 更多