Skip to content
Ritchie Cai edited this page Sep 30, 2015 · 5 revisions

Overview

core.matrix supports the concept of views as a tool for inspecting and modifying portions of larger arrays. Many core.matrix functions that return a subset of a larger array will return a view.

Single row/column

Mutable view of a single row/column

(ns test.core
  (:require [clojure.core.matrix :as mat]))
(def m (mat/new-matrix 3 4))
;; #vectorz/matrix [[0.0,0.0,0.0,0.0],
;; [0.0,0.0,0.0,0.0],
;; [0.0,0.0,0.0,0.0]]

(mat/assign! (mat/get-row m 0) (mat/array [1 2 3 4]))
;; #vectorz/matrix [[1.0,2.0,3.0,4.0],
;; [0.0,0.0,0.0,0.0],
;; [0.0,0.0,0.0,0.0]]

(mat/assign! (mat/get-column m 2) (mat/array [5 6 7]))
;; #vectorz/matrix [[1.0,2.0,5.0,4.0],
;; [0.0,0.0,6.0,0.0],
;; [0.0,0.0,7.0,0.0]]

Submatrix

(mat/assign! (mat/submatrix m [[0 3] [2 2]]) (mat/matrix [[8 9]
                                                          [10 11]
                                                          [12 13]]))
;; #vectorz/matrix [[1.0,2.0,8.0,9.0],
;; [0.0,0.0,10.0,11.0],
;; [0.0,0.0,12.0,13.0]]

Matlab equivalent of above code:

m(1:3, 3:4) = [8 9; 10 11; 12 13]

Index format for sub matrix is

(submatrix m [[row-start row-length] [col-start col-length]])