ndarray rust, change values of a slice

488 views Asked by At

In python's numpy given an array

a = np.zeros((10,10,2))

I could modify values of the array corresponding to a slice 4:6,: as such:

a[4:6,:] = [0,255]

In rust: given a ndarray from the ndarray package, i can slice

let mut img = Array3::<u8>::zeros((10,10,2));
let slice = img.slice_mut(s![4..6,..,..]);

But what is the idiomatic way of changing the values of that slice, by broadcasting the subarray corresponding to the last axis i.e. [0,255]?

1

There are 1 answers

0
Conformal On

through other means i found answer and im posting it here. One needs to combine slice_mut with method .assign(&stuff to broadcast)

 let mut img = Array3::<u8>::zeros((10,10,2));
 let arr = Array::ones(2);
 img.slice_mut(s![4..6usize,..,..]).assign(&arr);