Suppose I've got:
'[[c c c]
[y y y]
[m m m]]
and
'[[r g b]
[r g b]
[r g b]]
and I'd like to have:
'[[[c,r] [c,g] [c,b]]
[[y,r] [y,g] [y,b]]
[[m,r] [m,g] [m,b]]]
What is the elegant way to do this in clojure?
Consider adopting core.matrix for stuff like this.
It will manipulate nested vectors as matrices quite happily, but also does much more powerful stuff if you need it (for example, support for accelerated native matrix libraries via JBLAS). It's shaping up to be the definitive library/API for matrix computations in Clojure.
In this case you can simply use "emap" to apply a function element-wise to two matrices:
(use 'core.matrix)
(def cym '[[c c c]
[y y y]
[m m m]])
(def rgb '[[r g b]
[r g b]
[r g b]])
(emap vector cym rgb)
=> [[[c r] [c g] [c b]]
[[y r] [y g] [y b]]
[[m r] [m g] [m b]]]
partition
and interleave
get you there, as seqs:
(def a '[[c c c]
[y y y]
[m m m]])
(def b '[[r g b]
[r g b]
[r g b]])
(map (partial partition 2) (map interleave a b))
;=> (((c r) (c g) (c b))
; ((y r) (y g) (y b))
; ((m r) (m g) (m b)))
If for some reason you need to convert the answer to nested vectors, it might be worth a look at this question.