O'Reilly logo

Clojure for Machine Learning by Akhil Wali

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generating matrices

If the number of rows and columns in a matrix are equal, then we term the matrix as a square matrix. We can easily generate a simple square matrix of Generating matrices size by using the repeat function to repeat a single element as follows:

(defn square-mat
  "Creates a square matrix of size n x n 
  whose elements are all e"
  [n e]
  (let [repeater #(repeat n %)]
    (matrix (-> e repeater repeater))))

In the preceding example, we define a closure to repeat a value n times, which is shown as the repeater. We then use the thread macro (->) to pass the element e through the closure twice, and finally apply the matrix function to the result of the thread macro. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required