【问题标题】:NetLogo: How do I build a new vector based on randomly chosen values from another vector?NetLogo:如何根据从另一个向量中随机选择的值构建一个新向量?
【发布时间】:2022-01-06 17:15:42
【问题描述】:

我有一个 10 位数字的向量“原始”。现在我想创建基于“原始”的矢量“适应”。 'adapted' 应该在同一位置从 'original' 中取 n 个大于 0 的随机值,并用 0 填充其余的值,例如:

原始 = [2 3 6 2 0 5 7 2 4 8] 适应 = [2 0 0 0 0 5 0 2 0 0]

to go

  let n 3
  let vector-dimension 10

  let original []
  repeat vector-dimension
    [set original lput random 10 original]

   print original

   
   let adapted []

   while [sum (map [ [v1] -> ifelse-value (v1 > 0) [1] [0] ] (adapted)) != n]
     [set adapted (map [ [v1] -> ifelse-value ( (vector-dimension / n) * (100 / vector-dimension) > random-float 100) [v1] [0] ] (original)) ]
    
   print adapted
   
end

此代码有效,但速度很慢。我怎样才能更快地做到这一点?

【问题讨论】:

    标签: netlogo


    【解决方案1】:

    怎么样:

    to-report report-rand-n [ base n ]
      let indices ( range 0 (length base)) 
      let subset n-of n indices
      let out ( map [ [ i v ] -> ifelse-value ( member? i subset ) [v] [0] ] indices base)
      report out
    end
    

    此报告者创建一个索引列表(0 到传递的 base 的长度),然后随机选择 n 传递给 ifelse-value 的索引数量以返回基数中的原始值(如果 @ 987654325@ 是选定的indices) 或 0 之一。

    测试:

    to test
      let original [2 3 6 2 0 5 7 2 4 8]
      print report-rand-n original 3
      print report-rand-n original 3
      print report-rand-n original 5
      print report-rand-n original 5
    end
    
    observer> test
    [2 0 6 0 0 0 0 0 4 0]
    [2 0 0 0 0 0 0 2 0 8]
    [2 0 0 0 0 5 0 2 4 8]
    [0 0 6 2 0 5 0 0 0 8]
    

    编辑:

    to test
      let original [2 3 6 2 0 5 7 2 4 8]
      print word "testing: " original
      print report-rand-n original 3
      
      let few-digits [ 0 0 0 1 0 0 0 ]
      print word "testing: " few-digits
      print report-rand-n few-digits 3
      print ""
    
    end
    
    to-report report-rand-n [ base n ]
      ; create list of indices
      let indices ( range 0 (length base)) 
      
      ; To address point 1) in your comment:
      ; keep only indices that correspond to a value > 0 in base
      let indices-over-zero filter [ i -> item i base > 0 ] indices
      
      ; To address point 2 in your comment:
      ; If the length of indices over zero is less than n, replace n
      ; with the length of indices over zero
      if length indices-over-zero < n [
        set n length indices-over-zero
      ]
      let subset n-of n indices-over-zero
      let out ( map [ [ i v ] -> ifelse-value ( member? i subset ) [v] [0] ] indices base)
      report out
    end
    

    【讨论】:

    • 这绝对是更快的方式。谢谢!我怎样才能确保:1)从原始值中选取大于 0 的 n 个值?在您提供的原始向量中是 0。我从不希望在新向量中使用 0,但总是需要 3 个其他值。 2)如果数字 > 0 比 n 少,那么所有 > 0 的数字都会被选中,然后其余的数字都用 0 填充? 3)还有一种方法可以创建一个版本,其中我不选择原始向量的 n 个,而是选择最大/最小的 n 个数字,例如最大的 3 个数字还是最小的 3 个数字?我似乎无法让 max-n-of 和 min-n-of 与您的代码一起使用。
    • @Chris1234 - 查看第 1 点和第 2 点的潜在选项的编辑。对于第 3 点,我想我不明白,因为 max-n-of / min-n-of 已经做了你想做的事'正在自己描述。
    • 完美,解决了我所有的问题。谢谢你。我遇到了 m-n-of 和 min-n-off 的问题,因为它们需要一个代理集(向量不是一个),但我想通了。
    猜你喜欢
    • 2012-03-13
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    相关资源
    最近更新 更多