2017-02-15 67 views
-1

我有一個我想要轉換爲線段的10個概率列表,因此我可以執行輪盤選擇。我如何轉換爲線段?netlogo將概率轉換爲行值

概率列表: [0.17 0.15 0.14 0.14 0.11 0.1 0.06 0.06 0.04 0.03]

當轉換成線段應該是: [0.17 0.32 0.46 0.60 0.71 0.81 0.87 0.93 0.97 1.0]

現在這是我的代碼:

to calculate-s 
let i 1 ;; previously added a 0 to list using set p-list fput 0 p-list 
while [i < 11] [ 
    ask turtles [ 
    if [p] of self = item i p-list [ 
     let s (p + item (i - 1) p-list) 
    ] 
    ] 
    set i (i + 1) 
] 
end 

但是,當然,這只是總結了當前的概率和前一個,所以我得到: [0.17 0.32 0.29 0.28 etc]

+1

看起來是http://stackoverflow.com/questions/33570658/how-make-a-list-of-cumulative-sum-in-netlogo的副本 – Alan

回答

-1

我不知道你所說的線段的意思是什麼,但如果你只是想創建一個列表中的自包含塊,其中

newlist[i] = (oldlist[i] + (newlist[i - 1]))

您可以使用foreach步驟通過舊列表並生成一個新的總結值列表,如下所示。

to make-segments 

    let oldlist [0.17 0.15 0.14 0.14 0.11 0.1 0.06 0.06 0.04 0.03] 
    let newlist [] 

    let n 0 

    foreach oldlist [ 
    [x]-> 
    ifelse n < 1 [ ;;; if index is 0 you can't index 0 -1, so just use item 0 
     set newlist lput item 0 oldlist newlist 
    ] 
    [ ;;; else, add the item n from the old list to the 
     ;;; previous item in the new list. 
     set newlist lput (precision (item n oldlist + item (n - 1) newlist) 2) newlist 
    ] 
    set n n + 1 
    ] 

    print newlist 

end