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

Set Matrix diagonal elements

Tags: None
(comma "," separated)
donnss
Registered Member
Posts
7
Karma
0

Set Matrix diagonal elements

Mon Mar 14, 2016 4:10 pm
I know it it is possible to set a matrix's diagonal elements to a constant like this:

Code: Select all
Matrix<float, 100, 100> m;
m.diagonal().setConstant(9);


However, I want to set the adjacent diagonal elements to particular constants too, i.e. I want to take not the main diagonal elements but the diagonal elements where each of the elements is one position below the main diagonal. Is there a way to do it without looping?

EDIT: I have figured out one possible solution:

Code: Select all
m.block(1, 0, 99, 99).diagonal().setConstant(100);


Besides this, is there a "better" way to do it?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
yes, mat.diagonal(-1) or mat.diagonal(1) to get the sub/super diagonals, and of course you can get the other diagonals too... See: http://eigen.tuxfamily.org/dox-devel/cl ... 3dd988a7f7


Bookmarks



Who is online

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