如何在NetLogo中洗牌矩阵的顺序?

如何在NetLogo中洗牌矩阵的顺序?,netlogo,Netlogo,如何洗牌矩阵的内容并将其保留为矩阵?shuffle函数适用于列表,但不适用于矩阵 show shuffle [1 2 3 4 5] 1 2 3 5 4 set m matrix:from-row-list [[1 2 3 4 5]] show shuffle m SHUFFLE期望输入为列表,但得到 org.nlogo.extensions.matrix.MatrixExtension$LogoMatrix{{matrix:[[ 1 2 3 4 5]]}} 可能有一种更简单的方法,但您

如何洗牌矩阵的内容并将其保留为矩阵?shuffle函数适用于列表,但不适用于矩阵

show shuffle [1 2 3 4 5]
1 2 3 5 4

set m matrix:from-row-list
  [[1 2 3 4 5]]

show shuffle m
SHUFFLE期望输入为列表,但得到 org.nlogo.extensions.matrix.MatrixExtension$LogoMatrix{{matrix:[[ 1 2 3 4 5]]}}


可能有一种更简单的方法,但您可以跳转到列表,洗牌,然后根据原始矩阵的维度返回矩阵

extensions [ matrix ]

to setup
  ca
  let m matrix:from-row-list [ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 9 ] ]
  let sm shuffled-matrix m
  print matrix:pretty-print-text sm
  reset-ticks
end

; Reporter returns a shuffled matrix
to-report shuffled-matrix [ mat ]
  ; Get the number of columns
  let cols last matrix:dimensions mat

  ; Shuffle the matrix values as a list
  let shuf-vals shuffle reduce sentence matrix:to-row-list mat 

  ; Use the shuffled values to generate a new matrix
  ; with the same dimensions as the original
  report matrix:from-row-list ( subsetter shuf-vals cols )
end

; Reporter returns a list cut into sublists 
; based on the len value passed
to-report subsetter [ ls len ]
  ; Generate subsetting indices for the sublists
  let vals ( range 0 ( length ls ) len )

  ; Make subsets of ls based on the subsetting indices
  report map [ i -> sublist ls i ( i + len ) ] vals
end
设置
的几个示例输出:

[[ 1  6  7 ]
 [ 9  5  8 ]
 [ 3  4  2 ]]

[[ 4  6  8 ]
 [ 1  9  2 ]
 [ 7  5  3 ]]

[[ 2  9  4 ]
 [ 6  3  8 ]
 [ 5  1  7 ]]