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.
Asked
Active
Viewed 820 times
3
-
What have you tried or researched? As it is, you're simply asking for code without showing minimal understanding of the problem at hand, which makes the post off topic for SO. – t0mppa Feb 02 '14 at 22:49
1 Answers
2
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 ] ]}}

Seth Tisue
- 29,985
- 11
- 82
- 149