3

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?

2
  • It looks like your question might be answered by the answers of How to convert a collection of Vec<ndarray::Array1> into an Array2?. If not, please edit your question to explain the differences. Otherwise, we can mark this question as already answered. Commented Jan 20, 2020 at 13:21
  • @Shepmaster This does not answer my question, the answer basically recommends iterating through every element and just copying them. This could be said of almost any data structure and I would presume the inference could be made I am looking for some reasonably efficient cast, not simply looping through every value (which is what I am doing already). Commented Jan 20, 2020 at 14:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.