【问题标题】:Rust ndarray - Randomly fill ndarray wish shape I specifyRust ndarray - 随机填充我指定的 ndarray 愿望形状
【发布时间】:2021-06-26 22:16:57
【问题描述】:

我需要用我指定的形状制作一个二维数组并随机填充。

我尝试了下面的代码,但它对我大喊大叫

pub fn fill_shape(x:i32 , y:i32){
    let mut rng = rand::thread_rng();
    let mut a = vec![];
    for x in 0..(x*y)
    {
        a.push(rng.gen_range(0.0..1.0));
    }
    let mut temp = ndarray::Array::from_shape_vec((x, y),a);
}

this is why it gets upset

当我执行 (2,2) 之类的操作时它会起作用 错误信息

error[E0277]: the trait bound `(i32, i32): Dimension` is not satisfied
  --> src\mynn.rs:42:20
   |
42 |     let mut temp = ndarray::Array::from_shape_vec((x, y),a);
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Dimension` is not implemented for `(i32, i32)`
   |
   = note: required because of the requirements on the impl of `From<(i32, i32)>` for `StrideShape<(i32, i32)>`
   = note: required because of the requirements on the impl of `Into<StrideShape<(i32, i32)>>` for `(i32, i32)`
   = note: required by `ndarray::impl_constructors::<impl ArrayBase<S, D>>::from_shape_vec`

【问题讨论】:

  • 欢迎来到 SO。请发布错误消息。
  • 添加了错误信息,@SakuraKinomoto
  • 我看到了,谢谢你这样做。我不是 rust 专家,我建议您将错误发布给有更好知识的人可以帮助您。

标签: rust


【解决方案1】:

可以转换为Dimensions 并因此用作期望Into&lt;StrideShape&gt; 的参数的类型仅限于Ixs,它是usize 的别名。见IntoDimension

xy 转换为usize

ndarray::Array::from_shape_vec((x as usize, y as usize), a)

或者,如果合适,为您的 fill_shape 函数更改它们:

pub fn fill_shape(x: usize, y: usize) {

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2021-03-29
    • 1970-01-01
    • 2018-05-13
    • 2021-06-27
    • 2020-09-23
    相关资源
    最近更新 更多