如何对矩阵的内容进行洗牌,并将其保存为矩阵?洗牌功能适用于列表,但不适用于矩阵。
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洗牌期望输入是一个列表,但是得到了org.nlogo.extensions.matrix.MatrixExtension$LogoMatrix {{矩阵:[1 2 3 4 5 ]}}。
发布于 2019-04-16 18:25:42
也许有一个更简单的方法,但你可以跳到一个列表,洗牌,然后回到一个矩阵的基础上,你原来的矩阵。
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来自setup的几个示例输出
[[ 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 ]]https://stackoverflow.com/questions/55707286
复制相似问题