Eigen::RowVector Iterator
Asked Answered
C

1

7

Could someone please tell me how on earth I can iterate an Eigen::RowVectorXf?

I have looked for 3 hours through the web and documentation, and all I could find is from this link that I can access it by:

vector(i)
vector[i]

I have a:

auto vec = std::make_shared<Eigen::RowVectorXf>( rowIndex.size() ); 

Which I wanna populate with word frequencies.

Eigen::RowVectorXf::InnerIterator it(vec); it; ++it

Doesn't work, and

Eigen::RowVectorXf::Iterator it(vec); it; ++it

Doesn't exist.

The only thing that seems to work is:

for ( int i = 0; i < vec->row( 0 ).size(); i++ )
{
    std::cout << vec->row( 0 )[i] << std::endl;
}

Which IMHO seems bizzare, as I shouldn't have to explicitly point to row( 0 ) since this is a RowVector.

Isn't there a cleaner, faster or more elegant way?

Chatwin answered 29/5, 2013 at 22:28 Comment(1)
One thing that may be helpful is that there is no such thing as a "vector" in eigen. Eigen vectors are typedefs for matrices; and I quote: "Vector4f is a vector of 4 floats (Matrix<float, 4, 1>) RowVector3i is a row-vector of 3 ints (Matrix<int, 1, 3>)" I would still like to iterate through the matrix. I've looked for hours, too; can't find anything. I tried reading the source, and that's pretty awful.Funches
C
10

No need for the row(0), you can either use ->coeff(i) (not recommended because it skips the assertions, even in debug mode), or use operator* to dereference your shared_pointer:

for(int i=0; i<vec->size(); ++i)
  cout << (*vec)[i];

You can also use an InnerIterator, but you have to dereference your shared_pointer:

RowVectorXf::InnerIterator it(*vec); it; ++it
Cochleate answered 30/5, 2013 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.