How to vectorize a matrix?

help

#1

I’m beginning with Rust and nalgebra so I don’t read code documentions very well yet. I’m looking for an operator to “vectorize” / “stack” a matrix into a vector. Similar to a matrix(:) or reshape(matrix, [], 1) in matlab. So this question is similar to https://github.com/sebcrozet/nalgebra/issues/350 but I’m looking for a function with signature DMatrix -> DVector.
How should I proceed?


#2

I think .resize(m*n,1,0.0).column(0) is doing what I’m looking for :slight_smile: but happy to know about alternatives if someone has some.
EDIT: It’s wrong indeed, cf sebcrozet answer


#3

resize is actually not the same as reshape since it will extract only the first column and fill the rest with copies of val. You may want to consider something like that instead:

DVector::<f32>::from_column_slice(m * n, matrix.as_slice())

This will actually fill the vector with the entries of the matrices listed in column-vector order.


#4

Thank you! I was going crazy with my Gauss Newton optimization not converging as it should, and that was because of this! Input matrices were a bit big so I hadn’t printed the vectorized one and didn’t realize that there were 0 everywhere.