Jonathan Woollett-light
Jonathan Woollett-light

Reputation: 3243

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?

Upvotes: 3

Views: 1250

Answers (1)

zingi
zingi

Reputation: 1367

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

Upvotes: 1

Related Questions