Create new dynamic matrix from column slice

I have dynamic Matrix let capital_a = nalgebra::DMatrix::from_column_slice(2, 4, &[1.0f32, 2.0, 1.0, 0.5, 1.0, 0.0, 0.0, 1.0]); and want to crate a a new matrix from the last two columns.
I succeeded in getting one slice with let mut capital_b = lp.capital_a.column(basic_indexes[0]);

But adding a new column with capital_b.insert_column(1, 0.0); does not work and assigning the elements individually with capital_b[(0,0)] = 3.0; leads to compile errors. Did I do something wrong there?

Is there a better way to construct a matrix out of several columns of another matrix?

Hi!

The simplest way to extract the last two columns of the matrix is to use .columns(first_column, num_columns). In your case:

// Starting with the third column (at index 2), extract 2 columns.
let columns = capital_a.columns(2, 2);

And if you want a DMatrix instead of a matrix slice you can call columns.into_owned().

Thank you, now it works!

I cannot use the columns method, because I don’t know the indices beforehand. But now I can work around it.