Julius Canute
Julius Canute

Reputation: 221

Boost Matrix Dynamic Size Expansion

I am writing a c++ program which uses boost library for matrix operations. I have a need were by i have to dynamically expand the size of the initial matrix.

Example: if my matrix size was:

   matrix<float> m(3,3);

and later my matrix will expand and i'll need a 4*4 matrix. The naive approach i could think of is allocate a new matrix with size 4,4 and and copy all elements of 3*3 matrix to it. Isn't there any better way of doing this in boost?

Upvotes: 0

Views: 1161

Answers (2)

Kerrek SB
Kerrek SB

Reputation: 477040

Please, consider using the resize() function: "The existing elements of the matrix are preseved (sic) when specified."

Here is an example code from Boost.

This is one of ways.

matrix<int> A;  // Matrix size would be zero by zero
A.resize(2, 3);  // Matrix size became 2 by 3

Upvotes: 2

anio
anio

Reputation: 9161

Why not just create a matrix using the no arg constructor and call the resize method as needed?

http://www.boost.org/doc/libs/1_47_0/libs/numeric/ublas/doc/matrix.htm

Upvotes: 1

Related Questions