Building up larger matrices a la Matlab in core.matrix

46 views
Skip to first unread message

László Török

unread,
Jan 7, 2015, 1:18:37 PM1/7/15
to numerica...@googlegroups.com
Hi,

I've started using core.matrix as I've been missing the power of Matlab in my daily clojure work. It has been a fantastic experience so far.

However, I've been missing the elegant and succinct way to combine smaller matrices into larger ones a la Matlab.


I tried
(join
  [1 2]
  (slices [[3 4] 
              [5 6]])
  [7 8])

which is a bit more verbose than I wanted to, plus it complains about dim. mismatch. That goes away, if I do (join (concat ...)).

Ideally, I would want to do

(some-clever-join-op [[A B]
                                   [C D]])

and get a matrix that is a combined result of A-D matrices glued together along those dimensions. 
Is there already an elegant way of doing this in core matrix?

Thanks!

Laszlo

Mike Anderson

unread,
Jan 7, 2015, 8:20:58 PM1/7/15
to numerica...@googlegroups.com
On Thursday, 8 January 2015 02:18:37 UTC+8, László Török wrote:
Hi,

I've started using core.matrix as I've been missing the power of Matlab in my daily clojure work. It has been a fantastic experience so far.

However, I've been missing the elegant and succinct way to combine smaller matrices into larger ones a la Matlab.


I tried
(join
  [1 2]
  (slices [[3 4] 
              [5 6]])
  [7 8])

which is a bit more verbose than I wanted to, plus it complains about dim. mismatch. That goes away, if I do (join (concat ...)).

join is the right thing to use: the reason it doesn't work here is that you are joining a vector to a sequences of vectors and then to another vector. "join" recognises that the sequence of vectors is a mismatch, hence the error.


Ideally, I would want to do

(some-clever-join-op [[A B]
                                   [C D]])

and get a matrix that is a combined result of A-D matrices glued together along those dimensions. 
Is there already an elegant way of doing this in core matrix?

You can do something like:

(defn join-all
     "Joins a matrix of matrices to create a bigger matrix :-)"
     ([M]
       (apply join (map #(apply join-along 1 %) (slices M))))) 

I think this does what you want?

(join-all [[ [[1]] [[2 3]]]
              [ [[4] [7]] [[5 6] [8 9]]]])
=> [[1 2 3] [4 5 6] [7 8 9]]


László Török

unread,
Jan 9, 2015, 9:55:47 AM1/9/15
to numerica...@googlegroups.com
Hi, 

thanks! For some reason, I did not get email updates delivered to my inbox, but I ended up doing sg similar:

(defn join-all [m-of-m]
  (apply m/join
         (for [row m-of-m]
           (apply m/join-along (cons 1 row)))))
thx
Las

Mike Anderson

unread,
Jan 9, 2015, 8:21:14 PM1/9/15
to numerica...@googlegroups.com
Yep that should work....though you are making the (slightly) risky assumption that m-of-m is seqable, which not all implementations are.

For safety, it is best to use "slices" if you want to turn a matrix into a seq of its rows.....

László Török

unread,
Jan 10, 2015, 5:48:28 AM1/10/15
to numerica...@googlegroups.com

Good catch, thanks for pointing out!

--
You received this message because you are subscribed to the Google Groups "Numerical Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numerical-cloj...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages