1

Given a nested vector A, which is the 3 x 4 matrix

[[1 4 7 10] [2 5 8 11] [3 6 9 12]]

Transform A such that the nested vector (matrix) is now 2 x 6.

The output would look like

[[1 3 5 7 9 11] [2 4 6 8 10 12]]

As of now I am stuck on the beginning implementation of this idea.

4

2 回答 2

2

您可能想查看core.matrix

;; using [net.mikera/core.matrix "0.18.0"] as a dependency
(require '[clojure.core.matrix :as matrix])

(-> [[1 4 7 10] [2 5 8 11] [3 6 9 12]]
  (matrix/transpose)
  (matrix/reshape [6 2])
  (matrix/transpose))
;= [[1 3 5 7 9 11] [2 4 6 8 10 12]]
于 2013-12-27T19:14:09.307 回答
2

此函数将重塑 m 以由具有所需形状的子向量组成

(defn reshape [m & shape]
    (reduce (fn [vecs dim]
                (reduce #(conj %1 (subvec vecs %2 (+ dim %2)))
                        [] (range 0 (count vecs) dim)))
            (vec (flatten m)) (reverse shape)))

例子:

(reshape [1 [2 3 4] 5 6 7 8] 2 2) => [[[1 2] [3 4]] [[5 6] [7 8]]]
于 2019-08-28T15:36:12.690 回答