Best way to grow a matrix?

I have a routine where I am adding an unknown amount of rows to a Matrix. What is the fastest, most efficient way to grow the Matrix? Anything better than:

Matrix<double> origDb = Matrix.Build.Dense(22, 22);
...
int additionalRows = 22;
Matrix<double> newDb = Matrix.Build.Dense(origDb.RowCount + additionalRows, origDb.ColumnCount);
newDb.SetSubMatrix(0, 0, origDb);
origDb = newDb;

?

Essentially yes. We should make this a one liner though - I’m considering to add a Resize method or something similar.

Note that this is fairly expensive though, so if you need to do this incrementally many times without actually requiring a matrix in between, maybe collecting rows in a list is more efficient (and if at some point a matrix is needed, it can be easily created from a list of rows).

1 Like

I’ve added a Resize function, available in v4.14 and newer.

1 Like