2015-02-09 33 views
0

假設我有一個Clojure的矩陣A這樣(格式化爲清楚起見)更新矩陣Clojure中

[[1 4 3] 
[1 7 3] 
[1 8 3]] 

現在假設我想更新代替第一列中,由例如由兩個因素相乘,使新的矩陣變成

[[2 4 3] 
[2 2 3] 
[2 8 3]] 

一個怎樣Clojure中做到這一點?我曾嘗試之類的東西assoc和類似的東西

(join-along 1 (* (slice A 1 0) 2) (select A [0 1 2] [2 3]))

當然,沒有工作。如果矩陣有assoc這樣的矩陣,那將是很好的。

(massoc A [rows] [columns] replacement-vector) 

或Python中的一些簡單的像numpy

A[:,0]*2 = [[2 4 3] 
      [2 2 3] 
      [2 8 3]] 

感謝

+0

這更類似於'在Python語法numpy',而不是MATLAB。我只是挑剔。 – rayryeng 2015-02-09 20:23:52

回答

1

你應該看看clojure.core/matrix,看看它是否支持這樣的操作。

下面是可能是你要找的東西。在應用函數後,將其更改爲assoc是一個新值,而不是更新應該是微不足道的。

(defn mupdate-in 
    "Update all `coll' rows at `column' with `f'" 
    [coll column f & args] 
    (reduce #(apply update-in %1 [%2 column] f args) 
      coll 
      (range (count coll)))) 

一個例子:

(def m [[1 4 3] 
     [1 7 3] 
     [1 8 3]]) 

(mupdate-in m 0 * 2) 
;; [[2 4 3] 
;; [2 7 3] 
;; [2 8 3]] 

(mupdate-in m 2 + 10) 
;; [[1 4 13] 
;; [1 7 13] 
;; [1 8 13]]