(map #(apply max %) [[1 2 3] [2 5 1] [4 2 6]])
this will give you,
(3 5 6)
Regards,
--
Nurullah Akkaya
http://nakkaya.com
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
To unsubscribe, reply using "remove me" as the subject.
The `map` function allows you to specify more than one collection.
With multiple collections, the mapping function is passed an argument
for each collection. So:
(map + [1 2 3] [4 5 6])
=> ((+ 1 4) (+ 2 5) (+ 3 6))
=> (5, 7, 9)
By combining this with `apply` and `max`, you can find the maximum
value of each column:
(defn max-columns [coll]
(apply map max coll))
If we pass your vector to max-columns:
(max-columns [[1 2 3] [2 5 1] [4 2 6]])
=> (apply map max [[1 2 3] [2 5 1] [4 2 6]])
=> (map max [1 2 3] [2 5 1] [4 2 6])
=> ((max 1 2 4) (max 2 5 2) (max 3 1 6))
=> (4 5 6)
- James
(apply interleave [[1 2 3] [2 5 1] [4 2 6]])
you will get,
(1 2 4 2 5 2 3 1 6)
then if you partition by 3,
(partition 3 (apply interleave [[1 2 3] [2 5 1] [4 2 6]]))
you get,
((1 2 4) (2 5 2) (3 1 6))
then as before applying max,
(map #(apply max %)
(partition 3 (apply interleave [[1 2 3] [2 5 1] [4 2 6]])))
will give you,
(4 5 6)
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
To unsubscribe, reply using "remove me" as the subject.
(defn transpose [xs]
(apply map vector xs))
Now Nurullah's original suggestion applies:
(map #(apply max %) (transpose xs))
-Per
> http://rosettacode.org/wiki/Matrix_transposition#Clojure
>
> Does anyone know if transpose exists in core or contrib? A cursory check doesn't reveal it, seems like it should be available.
Not that I know. But here's a version that works even for higher nesting levels than 2 (look for nv-transpose):
http://code.google.com/p/clj-multiarray/source/browse/src/multiarray/nested_vectors.clj
Konrad.