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

Eigen::Map and Pointer to a Submatrix

Tags: None
(comma "," separated)
newbx
Registered Member
Posts
1
Karma
0

Eigen::Map and Pointer to a Submatrix

Tue Aug 18, 2015 11:44 am
Hi.

I'm a newbie to Eigen; sorry if my question is stupid/vague!

Basically I need to have an Eigen::Matrix (or Eigen::Map that views an array as a Matrix) that is a pointer to a submatrix of another Eigen Matrix (say myMatrix, column-major).
In my code, myMatrix is always being updated after a series of costly calculations, and I need to have a pointer (e.g., Eigen::Map) that always gives me access to a particular submatrix.

For example myMatrix is 3x3, and I need to have a Matrix/Map that points to the e.g., top-left 2x2 block of myMatrix.

I've tried mapping
Code: Select all
myMatrix.block(0,0,2,2).data()

to a 2x2 Matrix, but this doesn't give me the top-left 2x2 block of myMatrix.

For example suppose myMatrix = [1 2 3; 4 5 6; 7 8 9]. Then I need a pointer to [1 2; 4 5], while the code above gives me [1 7; 4 2].

Thanks very much for your help in advance.
Tal
Registered Member
Posts
30
Karma
0
First, I'm not sure, but I think I know the answer.

In your case, Eigen Map stride is non-zero, therefore, data() isn't must to return a normal array, but only it's physical reference, Map
knows when to skip elements and how much, but you don't.

The all point of Map is to represent a (multi) sub-array from full array without copy anything.

You can think on this like not a physical memory, but a virtual memory.


User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
What you are looking for is Ref<>, for instance:

Ref<MatrixXd> sub = mat.block(1,2,3,4);

The use 'sub' as a Matrix or Map. Actually Map, Ref, and Block inherit from the same base class. You can also use Block:

Block<MatrixXd> sub(mat,1,2,3,4);


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar