1
假設我有一個3×3矩陣如何快速更新Incanter,Clojure中的矩陣元素?
(def myMatrix (matrix (range 9) 3))
; A 3x3 matrix
; -------------
; 0.00e+00 1.00e+00 2.00e+00
; 3.00e+00 4.00e+00 5.00e+00
; 6.00e+00 7.00e+00 8.00e+00
我可以使用$來獲取元素,說第二行第一列
($ 1 0 myMatrix) ; --> 3
是否有任何API的方法來快速更新的元素,然後返回矩陣?例如
(update-matrix-by-element 2 1 myMatrix 4)
; A 3x3 matrix
; -------------
; 0.00e+00 1.00e+00 2.00e+00
; 4.00e+00 4.00e+00 5.00e+00
; 6.00e+00 7.00e+00 8.00e+00
最接近的API方法我能看到的是bind-rows和bind-columns,我的當前版本的使用這兩種方法的功能是
;note i j starts from 1 rather than 0
(defn update-matrix-by-element [i j myMatrix value]
(if (or (> i (count (trans myMatrix))) (> j (count myMatrix)) (< i 1) (< j 1) (not (integer? i)) (not (integer? j)))
myMatrix
(let [n (count myMatrix)
m (count (trans myMatrix))
rangeFn #(if (== %1 %2) %1 (range %1 %2))
m1 (if (== (dec i) 0) []
($ (rangeFn 0 (dec i)) :all myMatrix))
m2 (if (== i m) []
($ (rangeFn i m) :all myMatrix))
matrixFn #(if (matrix? %) % [ %])
newRow (if (== (dec j) 0)
(bind-columns [value] (matrixFn ($ (dec i) (rangeFn j n) myMatrix)))
(if (== j n)
(bind-columns (matrixFn ($ (dec i) (rangeFn 0 (dec j)) myMatrix)) [value] )
(bind-columns (matrixFn ($ (dec i) (rangeFn 0 (dec j)) myMatrix)) [value] (matrixFn ($ (dec i) (rangeFn j n) myMatrix))))
)
]
; (prn " m1 " m1) (prn " m2 " m2) (prn " newrow " newRow)
(bind-rows m1 newRow m2))))