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

accessing sub-matrices in Eigen

Tags: None
(comma "," separated)
pamparana
Registered Member
Posts
18
Karma
0

accessing sub-matrices in Eigen

Sun Feb 28, 2016 9:09 pm
I am currently porting some matlab code to C++ and trying to use the Eigen library. I have a lot of matrix-vector multiplications that need to work on sub-matrices. For example, in matlab one can have do referencing as an example as follows:
Code: Select all
U(:, 1:n)


Here, I have a matrix U where I am selecting all the rows and 'n' columns where 'n' is only known during run-time. Is it possible to generate such a view into the data using Eigen efficiently?

The line that I need to port is something like this in matlab:

Code: Select all
A_hat = U(:, 1:n) * diag(diagS(1:n) - 1/mu) * V(:, 1:n)'


Here U and V are matrices and diagS is a vector which gets turned into a diagonal matrix using the diag() function in matlab.
pamparana
Registered Member
Posts
18
Karma
0

Re: accessing sub-matrices in Eigen

Sun Feb 28, 2016 9:41 pm
I realised there was a non-templates version of the block method. I have not tested it yet but I could do something like:

Code: Select all
X = U.block(0, 0, U.rows(), n) *
      singular_values.head(n).asDiagonal() *
      V.block(0, 0, V.rows(), n).transpose();
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: accessing sub-matrices in Eigen

Mon Feb 29, 2016 10:02 am
There are also convenient shortcuts, like U.leftCols(n). See them all there: http://eigen.tuxfamily.org/dox-devel/gr ... tions.html


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], ourcraft