I would like to create a multidimensional matrix in Rust (the product of which exceeds 1e6). I found the ndarray crate, however the documentation gives no explanation on how to use the ArrayD type which seems to correspond to my needs.
1 Answer
You can pass a &[usize] (or Vec<usize>) containing N values with the shape of the array to create an N-dimensional array to any constructor function which accepts the shape of the array, like Array::zeros. For example, the following code creates an Array with 9 dimensions of shape 4 * 7 * 6 * 5 * 2 * 10 * 9 * 3 * 8:
//! ```cargo
//! [dependencies]
//! ndarray = "*"
//! ```
extern crate ndarray;
use ndarray::ArrayD;
fn main() {
let mut array = ArrayD::zeros([4, 7, 6, 5, 2, 10, 9, 3, 8].as_ref());
array[[1; 9].as_ref()] = 123;
println!("{:?}", array[[0; 9].as_ref()]);
println!("{:?}", array[[1; 9].as_ref()]);
}
Output:
0
123