0

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)
madeinQuant
  • 1,721
  • 1
  • 18
  • 29

2 Answers2

3
(require '[clojure.core.matrix :as m])

(m/mset! A 0 0 -1)
akond
  • 15,865
  • 4
  • 35
  • 55
  • Thank you. May I ask a question? What is the difference between with or without (') in front of "["? – madeinQuant Dec 16 '20 at 13:08
  • I got the error message "No implementation of method: :set-2d!". I am using [incanter "1.5.7"] with clojure "1.10.1", incanter 1.9.3 is incompatable with clojure "1.10.1", Anyway, thanks for your help. – madeinQuant Dec 16 '20 at 13:48
1

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 
    
madeinQuant
  • 1,721
  • 1
  • 18
  • 29