user3264006
user3264006

Reputation: 49

How to make a matrix of random values in NetLogo?

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.

Upvotes: 3

Views: 845

Answers (1)

Seth Tisue
Seth Tisue

Reputation: 30453

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 ] ]}}

Upvotes: 2

Related Questions