Ndarray create array with shape from another array

When a is 3 dimensional ndarray this works ok:

let sh = a.shape();
let mut result = Array::<u16, Ix3>::zeros((sh[0], sh[1], sh[2]));

But it is not particularly satisfying. What is the ndarray idiomatic approach to create a new array from the dimensions of another array?

I tried some different combos with Dim<> but couldn't get there.

I don't personally know, but browsing docs, ArrayBase::shape says

Note that you probably don’t want to use this to create an array of the same shape as another array because creating an array with e.g. Array::zeros() using a shape of type &[usize] results in a dynamic-dimensional array. If you want to create an array that has the same shape and dimensionality as another array, use .raw_dim() instead:

// To get the same dimension type, use `.raw_dim()` instead:
let c = Array::zeros(a.raw_dim());

Great - thanks - that works

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.