Reputation: 3243
Using ndarray with 2 Array
structs, is there an effective way to swap 2 rows/cols/(slices along some axis)?
The title really sums up the question.
Upvotes: 1
Views: 688
Reputation: 8754
While different people might mean different things by "effective", a straightforward way is to assign the rows/columns to each other by means of a temporary. Here is an example for two rows (Playground):
let mut a = arr2(&[[1, 2, 3], [4, 5, 6]]);
let mut b = arr2(&[[7, 8, 9], [10, 11, 12], [13, 14, 15]]);
let mut a_row = a.slice_mut(s![1, ..]);
let mut b_row = b.slice_mut(s![2, ..]);
let tmp = a_row.to_owned();
a_row.assign(&b_row);
b_row.assign(&tmp);
println!("a = {:?}", a);
println!("b = {:?}", b);
Alternatively, you can use azip!()
to swap elementwise and avoid creating a temporary array, or even par_azip!
to parallelize the copying.
Upvotes: 2