如何访问特征向量矩阵< float,2,1>

时间:2014-05-13 17:38:55

标签: c++ eigen

我正在迭代一个由Vector

组成的向量
Matrix<float, 2, 1>

for(auto it = uvVertices.begin(); it != uvVertices.end(); ++it) {
    std::cout << *it;  
}

这给出了如下输出:

0.123120.212354

这是正确的,我怎么才能只访问第一个或第二个组件?所以我得到了

0.12312

http://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html这里有一个参考,但我无法想象出来。

2 个答案:

答案 0 :(得分:2)

如果您想获得容器的n - 元素,可以使用std::next,如下所示:

auto pos = 1; // Get the second element
auto it(std::next(uvVertices.begin(), k));
std::cout << *it;

只需取消引用uvVertices.begin()即可访问初始元素,如下所示:

std::cout << *(uvVertices.begin()); // Get the initial element

答案 1 :(得分:2)

如果我理解正确...你可以将迭代器解除引用到循环内的临时引用以方便和访问系数内部,就像任何Eigen对象一样:

for(auto it = uvVertices.begin(); it != uvVertices.end(); ++it) {
    Matrix<float, 2, 1>& v = *it;
    //auto& v = *it; // should also work
    std::cout << v(0,0);  
    std::cout << v(1,0);
}

你也可以使用range-for:

for(auto& v  : uvVertices) {
    std::cout << v(0,0);  
    std::cout << v(1,0);
}

我还会考虑将Eigen::Vector类型用于矢量。