Shall I collect from `map iter` as `ArrayBase`?

I create an ArrayBase by arr2() and then create another data by window and map, then last I collect the result as vec as below;

    let x = arr2(&[
        [1.2, 2.3],
        [3.23, 4.3],
        [3.2, 443.2],
        [32.2, 43.3],
        [3.3, 4.5],
        [2.3, 3.4],
    ]);
    let y = x
        .windows((3, 2))
        .into_iter()
        .map(|wi| [wi.column(0).sum(), wi.column(1).sum(), *wi.last().unwrap()]).collect::<Vec<_>>();

I want to know whether I can collect the result as ArrayBase directly, just like which is create by arr2?

Looking through the ndarray API, as far as I can tell, only 1-dimensional arrays support .collect() directly, hower, the creation of a 2-dimensional array (Array2) from a Vec<[T; N]> is super cheap, as far as I can tell, and works without any copying or moving of the data, i.e. the allocation of the Vec is just used as-is. So you can just do .collect::<Vec<_>>().into(); and annotate the Array2 type, and everything should work nicely and efficiently.

1 Like

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.