How to make a matrix of random values in NetLogo?

假装没事ソ 提交于 2019-12-10 20:15:03

问题


Is there a way to easily make an $n \cross m$ matrix in NetLogo? Additionally would it be possible to fill this matrix with random values? Thanks.


回答1:


this answer has been updated for NetLogo 6 task syntax

See http://ccl.northwestern.edu/netlogo/docs/matrix.html for docs on NetLogo's matrix extension.

For creating a matrix, there are several primitives that do that: matrix:make-constant, matrix:make-identity, matrix:from-row-list, matrix:from-column-list.

For creating a matrix and filling it with random values, I'd suggest defining this procedure first:

to-report fill-matrix [n m generator]
  report matrix:from-row-list n-values n [n-values m [runresult generator]]
end

Then to make, say, a 5 by 5 matrix, of, say, random integers in the range 0 to 9, it's:

fill-matrix 5 5 [-> random 10]

Example result:

observer> show fill-matrix 5 5 [-> random 10]
observer: {{matrix:  [ [ 5 9 3 2 6 ][ 5 8 2 8 0 ][ 6 7 3 7 4 ][ 7 0 4 6 3 ][ 7 9 0 0 5 ] ]}}


来源:https://stackoverflow.com/questions/21517015/how-to-make-a-matrix-of-random-values-in-netlogo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!