# How to change the dimensions of an Eigen Matrix in a loop?

I have a while loop, in which I use a Matrix A, vectors B and x with varying dimensions:

int count=0;
while(true){
count+=1;

MatrixXd A(count,2*count);
VectorXd B(2*count);

// assignment of A, and B
//

VectorXd x = A.householderQr().solve(B);

// output of x

if (x.norm()>30)
break;

}


However the program built this way always collapses. I tried to use pointers to such matrices or vector, and got similar failures.

How can I handle such issue?

• You may need to deallocate the memory before changing the dimensions of the matrix. What library are you using for your matrices/vectors? – Paul Dec 4 '13 at 16:14
• What do you mean by "collapse"? – ggael Dec 4 '13 at 17:05

You have a size mismatch issue: A is a count x 2*count matrix, and you are trying to solve Ax=B with B a 2*count x 1 vector.
Moreover, if you compile without -DNDEBUG, you should get a nice assertion telling you this is wrong.
A.resize(count, 2*count);