This forum has been archived. All content is frozen. Please use KDE Discuss instead.

function taking generic Eigen objects.

Tags: None
(comma "," separated)
martinakos
Registered Member
Posts
53
Karma
0
OS
Hi there,

I'm trying to write a generic function to get elements at certain 1D indexes
of a Eigen object, the indexes are given by another Eigen object. This is like the Matlab indexing one vector into another. I got it working for concrete types but not quite for the generic case and I've been trying already for quite a while.
Can someone can give me a hand?

The function looks like:

Code: Select all
template<class Derived1, class Derived2>
typename Derived1::PlainObject indexInto(const Eigen::EigenBase<Derived1> &data, const Eigen::EigenBase<Derived2> &index)
{
  typename Derived1::PlainObject result(1, index.size());
  for(int i = 0; i < index.size(); ++i)
    result(i) = data(static_cast<int>(index(i)));
  return result;
}


Thanks,
Martin
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
you should soon be able to do result = data(index), in the mean time:

typename Derived1::PlainObject result(data.rows(), index.size());
for(int i = 0; i < index.size(); ++i)
result.col(i) = data.col(index(i));
return result;
martinakos
Registered Member
Posts
53
Karma
0
OS
Oh! great news! I would be using result = data(index) a lot!

About the mean time solution, I still don't get it to work, now I've tried:
Code: Select all
    template<class Derived1, class Derived2>
    typename Derived1::PlainObject indexInto(const Eigen::EigenBase<Derived1> &data, const Eigen::EigenBase<Derived2> &index)
    {
      typename Derived1::PlainObject result(data.rows(), index.size());
      for(int i = 0; i < index.size(); ++i)
          result.col(i) = data.col(index(i));
      return result;
    }


I get a compile error
C2039: 'col' : is not a member of 'Eigen::EigenBase<Derived>' in the line
result.col(i) = data.col(index(i));

Actually, instead of copying the whole column I would like to access data at 1D indexes and put the result in a Vector or 1D Array depending on the data type.

Thanks
Martin
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
oh, you have to replace EigenBase by DenseBase
martinakos
Registered Member
Posts
53
Karma
0
OS
Thanks!


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]