How can I set set the values of a MatrixView to the values of a vector of the same size?

56 Views Asked by At

I have for example an SVector<f64, 10> to store data. While going over a loop I want to replace sections of length 2 in this vector with the values of a SVector<f64, 2>.

let a = SVector::<f64, 10>::default();

for i in 0..5 {
    let newvec = SVector::<f64, 2>::new(1.0, 2.0); 

    a.fixed_rows_mut::<2>(2 * i) = newvec; // why does this not work?

}

I basically have a large vector and need to set elements to certain values. Since fixed_rows_mut exists I would imagine this is the way to do it.

Reading the Docs I didn't find another obvious way to do it.

Edit: I fixed an error and another one so that this example can be used directly.

Edit: Here the error message:

error[E0070]: invalid left-hand side of assignment
 --> src/main.rs:9:38
  |
9 |         a.fixed_rows_mut::<2>(2 * i) = newvec;
  |         ---------------------------- ^
  |         |
  |         cannot assign to this expression

For more information about this error, try `rustc --explain E0070`.
error: could not compile `playground` (bin "playground") due to previous error

and a playground link.

1

There are 1 best solutions below

4
On BEST ANSWER

You can't assign a matrix to another matrix directly (which is what you are trying to do with a.fixed_rows_mut::<2>(2 * i) = newvec;). What you can do is assign the contents of your left-hand matrix to the contents of the right-hand matrix. To do that, you have to dereference both the left-hand side and the right-hand side of the assignment:

for i in 0..5 {
    let newvec = SVector::<f64, 2>::new(1.0, 2.0);

    *a.fixed_rows_mut::<2>(2 * i) = *newvec;
}

Playground.