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

How to set the diagonal elements of a matrix to a constant

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

I'm wondering why the following expression doesn't work for eigen:

Code: Select all
MatrixXd A(10,10);
A.setRandom(10,10); // put some data inside

A.diagonal().setZero(10);


while in the examples is possible to compute the sum simply as
Code: Select all
A.diagonal().sum();


I would like to set the diagonal elements of a matrix to zero, is it possible via Eigen API without an explicit loop?
jitseniesen
Registered Member
Posts
204
Karma
2
The following variant works:
Code: Select all
A.diagonal().setZero();

Your code, in which the size is specified, is not accepted because one cannot resize A.diagonal() . This is perhaps rather inconsistent in that we do allow "Vector3d v; v.setZero(3);" even though v is fixed size.


Bookmarks



Who is online

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