|
I'd like to swap the rows/columns of a given 2D ndarray idiomatically. I've found this question: #545, which explains that one can create two mutable array views, however it doesn't explain how this is done. I've done something hacky, like so, fn swap(mut keys: KeysViewMut, i: usize, j: usize) {
let mut row_x = keys.index_axis_mut(ndarray::Axis(0), 0);
row_x.swap(i, j);
{
let mut row_y = keys.index_axis_mut(ndarray::Axis(0), 1);
row_y.swap(i, j);
}
{
let mut row_z = keys.index_axis_mut(ndarray::Axis(0), 2);
row_z.swap(i, j);
}
{
let mut row_l = keys.index_axis_mut(ndarray::Axis(0), 3);
row_l.swap(i, j);
}
}To ensure that the borrows are in their own scope. |
Answered by
bluss
May 17, 2021
Replies: 1 comment
|
For example the rows_mut producer is an ndproducer and iterable, so it can give you array views of all the rows simultaneously. There's a good solution in using the |
0 replies
Answer selected by
skailasa
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For example the rows_mut producer is an ndproducer and iterable, so it can give you array views of all the rows simultaneously.
There's a good solution in using the
multi_slice_mut- you can cut out multiple slices however you want, and that didn't exist back then when the other question was asked.