Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen::RowVector Iterator

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?

like image 420
Ælex Avatar asked May 29 '13 22:05

Ælex


1 Answers

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
like image 81
ggael Avatar answered Oct 06 '22 00:10

ggael