Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding rows / columns to existing matrices in core.matrix (Clojure)

Tags:

matrix

clojure

How does one add a row or a column to an existing matrix? I'm trying to add a bias-term, a column of ones, as the first row of a matrix. In Octave I can do this with:

M = [ones(size(M, 1), 1), M];
like image 606
luxbock Avatar asked Aug 04 '26 21:08

luxbock


1 Answers

You can use the join function to append arrays along the major dimension.

And you can combine this with broadcast to get a matrix of ones in whatever size you like, e.g.:

e.g.

(join (broadcast 1 [1 3]) 
      [[1 2 3] 
       [4 5 6]   
       [7 8 9]])
=> [[1 1 1] 
    [1 2 3] 
    [4 5 6] 
    [7 8 9]]
like image 60
mikera Avatar answered Aug 07 '26 20:08

mikera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!