Closed abishekatp closed 1 year ago
Unfortunately, std::ops::Index
and std::ops::IndexMut
are somewhat limited, so indexing can only be used to get a single element. To get a slice (i.e. a view) of a portion of an array, you have to use separate slicing methods. See the Slicing overview in the docs. Then, you can call .assign()
to assign to that view. Here's an example:
use ndarray::prelude::*;
fn main() {
let mut d4_arr: Array4<f64> = Array4::zeros((2,2,3,2));
// eye can only be used for square arrays, so we instead create an array of
// zeros and then fill the diagonal with ones.
let mut d2_arr: Array2<f64> = Array2::zeros((3,2));
d2_arr.diag_mut().fill(1.);
// Slice to get a portion of the array, then .assign().
d4_arr.slice_mut(s![0, 0, .., ..]).assign(&d2_arr);
println!("{}", d4_arr);
}
Since you're coming from NumPy, you may also be interested in the ndarray
for NumPy users page.
@jturner314 Thank you for the answer. This will work for me. Also the NumPy users page is very useful. I am closing this issue.
How can I insert some 2D array into 4D array. For examples in python numpy we can do that like this
In Rust I have a situration