Take the 2-minute tour ×

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:


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?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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
share|improve this answer
Thank you! Will this work for setting values as well? –  Alex May 30 '13 at 21:31
Yes, of course. –  ggael May 30 '13 at 22:01
add comment

Your Answer


