Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conservativeResize() with zero values for the new values

Tags:

c++

eigen3

How to set the new values to zero after resizing a matrix? It is really weird that after resizing the matrix, the new values are set to trash values instead of at least set to zero.

N = 0;
Eigen::MatrixXd CO;
CO.setZero(3+3*N, 3+3*N);
std::cout << CO << std::endl << std::endl;
Nt = 1;
CO.conservativeResize(3+3*Nt,3+3*Nt);
std::cout << CO << std::endl << std::endl;

The result

enter image description here

like image 492
CroCo Avatar asked Aug 14 '14 21:08

CroCo


1 Answers

I've solved the problem by using conservativeResizeLike()

int Nt = 0;
Eigen::MatrixXd  CO;
CO.setOnes(3+3*Nt, 3+3*Nt);
std::cout << CO << std::endl << std::endl;
Nt = 1;
CO.conservativeResizeLike(Eigen::MatrixXd::Zero(3+3*Nt,3+3*Nt));
std::cout << CO << std::endl << std::endl;

The result

enter image description here

Also, I found out you can set them as ones Eigen::MatrixXd::Ones(3+3*Nt,3+3*Nt) or identity Eigen::MatrixXd::Identity(3+3*Nt,3+3*Nt)

For Identity

enter image description here

like image 190
CroCo Avatar answered Sep 21 '22 05:09

CroCo