Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust ndarray: Convert ArrayD to Array2

Got an ArrayD which always has 2 dimensions, but is an ArrayD due to a calculation, need to change this to Array2 for storage.

Been looking through documentation, can't seem to find a way.

Is there a function to do it?

like image 637
Jonathan Woollett-light Avatar asked May 10 '26 21:05

Jonathan Woollett-light


1 Answers

If you have a variable of type ArrayD<f32>, you can do:

// convert n-dimensional array into 2d array
let arr2d: Array2<f32> = arr2d.into_dimensionality::<Ix2>()?;

to convert it into a variable of type Array2<f32>.

If the dimensions don't match, it will throw an ndarray::ShapeError error.


also see: ndarray documentation: into_dimensionality

like image 161
zingi Avatar answered May 14 '26 10:05

zingi