How to change the matrix's element with incanter?

57 Views Asked by At

How to change a element in the matrix? According to Incanter document, the library is built on top of Clatrix. With Clatrix, set an element in the matrix with the command (set A 1 2 0). Please comment how to set the element in incanter. Thank you.

(ns cljsl.optimization
  (:require [incanter.core :as i]
            [incanter.stats :as s]))

;; create a matrix  
cljsl.examples=> (def A (i/matrix [[0 1 2] [3 4 5]]))

cljsl.examples=> A
 A 2x3 matrix
 -------------
 0.00e+00  1.00e+00  2.00e+00 
 3.00e+00  4.00e+00  5.00e+00 

;; the view the item 
cljsl.examples=> (i/$ 0 0 A)
0.0

;; element can be set with Clatrix, unfortunately, it don't correct with Incanter.
cljsl.examples=> (cl/set A 1 2 0)
2

There are 2 best solutions below

0
On

Thanks for the help. After review the book Clojure for Machine Learning and Clojure for Data Science. Found procedures to fix the error.

  1. adding the following dependency to the project.clj file.

    [clatrix "0.5.0"]
    
  2. The namespace declaration

    (ns cljsl.optimization
     (:require [clatrix.core :as cl]
               [incanter.core :as i]
               [incanter.stats :as s]))
    
  3. Testing

    cljsl.optimization=> (def A (i/matrix [[0 1 2] [3 4 5]]))
    #'cljsl.optimization/A
    cljsl.optimization=> A
    A 2x3 matrix
    -------------
    0.00e+00  1.00e+00  2.00e+00 
    3.00e+00  4.00e+00  5.00e+00
    
    ljsl.optimization=> (cl/set A 1 2 0)
    #object[org.jblas.DoubleMatrix 0x1c951881 "[0.000000, 1.000000, 2.000000; 3.000000, 4.000000, 0.000000]"]
    cljsl.optimization=> A
    A 2x3 matrix
    -------------
    0.00e+00  1.00e+00  2.00e+00 
    3.00e+00  4.00e+00  0.00e+00 
    
2
On
(require '[clojure.core.matrix :as m])

(m/mset! A 0 0 -1)