How to access Eigen Vector Matrix< float, 2, 1 >
Solution 1
If I understand correctly... You can just dereference the iterator to a temporary reference inside the loop for convenience and access coefficients inside like with any Eigen
object:
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);
}
You could also use range-for:
for(auto& v : uvVertices) {
std::cout << v(0,0);
std::cout << v(1,0);
}
I would also consider using Eigen::Vector
type for a vector.
Solution 2
If you would like to get the n
-th element of a container, you can use std::next
, like this:
auto pos = 1; // Get the second element
auto it(std::next(uvVertices.begin(), k));
std::cout << *it;
The initial element can be accessed simply by dereferencing uvVertices.begin()
, like this:
std::cout << *(uvVertices.begin()); // Get the initial element
user1767754
Updated on August 22, 2022Comments
-
user1767754 almost 2 years
I am iterating through a vector, which consists of Vector
Matrix<float, 2, 1> for(auto it = uvVertices.begin(); it != uvVertices.end(); ++it) { std::cout << *it; }
this gives an output like: which works
0.123120.212354
which is correct, how can i access only the first or the second component? So that i get
0.12312
http://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html here is a reference but i could not figur eit out.