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

Passing row of a matrix as a vector argument

Tags: None
(comma "," separated)
yoavhacohen
Registered Member
Posts
3
Karma
0
OS
Hi,

I have a function that gets a VectorXf as an argument:
Code: Select all
void foo(const VectorXf& v)
{
...
}


Now, when I pass a row of a matrix to this function by:
Code: Select all
Matrix<float, 3, Dynamic> m(10, 100);
...
foo(m.row(1)); // Doesn't work

It doesn't work.
But when I use:
Code: Select all
Matrix<float, 3, Dynamic> m(10, 100);
VectorXf m_row_1 = m.row(1);
foo(m_row_1); // Works


My question is why doesn't the first option work, and what's the best way to pass a row of a matrix to a function.

Thanks in advance,
Yoav
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
because .row() or .col() does not return a VectorXf but a lightweight Block<> object. See this page to write generic functions:

http://eigen.tuxfamily.org/dox/TopicFun ... Types.html
yoavhacohen
Registered Member
Posts
3
Karma
0
OS
Thanks a lot! This was very helpful.

Now, assuming that I want to write a template function that is limited to get only vectors, or BlockVectors.
1. Is there a common abstract class for all vectors (/segments)?
2. How can I write a function that deduces the size of the vector as a constant in compile time?

Thanks in advance,
Yoav
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
There is no common base class for that, but you can use a static assert or boost::enable_if. e.g.:

foo(const MatrixBase<Derived>& vec) {
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
}

The underlying compile time test is:

Derived::IsVectorAtCompileTime

You can use this with enable_if.


Useful compile-time information:

Derived::SizeAtCompileTime
Derived::RowsAtCompileTime
Derived::ColsAtCompileTime

Note that they might be equal to Dynamic if the size is not known at compile time!


Bookmarks



Who is online

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