我有一个矩阵m,我想有一个用户定义的函数,该函数使我可以控制与值相乘的元素。
函数matrix:set-and-report
看起来很有前途,但是我不确定如何对多个元素实现此功能。
例如,我想将矩阵的前3个元素乘以-1,从中移出:
let m matrix:from-row-list [1 2 3 4 5 6]
print m
对此:
let n matrix:from-row-list [-1 -2 -3 4 5 6]
答案 0 :(得分:3)
有了matrix:set-and-report
,您的确确实接近解决方案。请检查示例,希望这是您想要的。报告功能具有矩阵作为输入。与指定行相比,要从索引开始乘法,在何处终止,最后是乘法器。
Extensions [
matrix
]
to test
let m matrix:from-row-list [ [1 2 3 4 5 6] [1 2 3 4 5 6] ]
print (word "original matrix " m)
print (word "modified matrix " matrix-row-manipulation m 0 0 3 -1)
end
to-report matrix-row-manipulation [matrix row columen-index-start columen-index-end multiplier]
let index (range columen-index-start columen-index-end 1)
foreach index [ i ->
set matrix matrix:set-and-report matrix row i (matrix:get matrix row i * multiplier )
]
report matrix
end
这将使您返回:
observer> test
original matrix {{matrix: [ [ 1 2 3 4 5 6 ][ 1 2 3 4 5 6 ] ]}}
modified matrix {{matrix: [ [ -1 -2 -3 4 5 6 ][ 1 2 3 4 5 6 ] ]}}