-1

给定两个嵌套向量 x 和 y,其中 x 是

 (def x [[1 2] [3 4]])

并且 y 是

 (def y [[5 6] [7 8]])

如何将嵌套向量 x 和 y 沿数组连接到由附加输入 d 指定的维度?

即,给定 x、y 和 d=1,样本输出将是:

[[1 2] [3 4] [5 6] [7 8]]

其中 y 成为新嵌套向量的第三和第四行。

对于 d=1 我试过

(vec (concat [[1 2] [3 4]] [[5 6] [7 8]].

如果 d=2 初始 x 和 ya 样本输出将是:

[[1 2 5 6] [3 4 7 8]]

这是我最不确定的情况。

在 d=3 的情况下,x 和 y 将被单独留下,因为它们是 2 x 2。因此,x 和 y 将被原封不动地输出。

4

1 回答 1

1

core.matrix库非常适合沿任意维度对矩阵进行切片:

项目.clj:

(defproject hello "0.1.0-SNAPSHOT"                                 
  :description "FIXME: write description"                          
  :url "http://example.com/FIXME"                                  
  :license {:name "Eclipse Public License"                         
            :url "http://www.eclipse.org/legal/epl-v10.html"}      
  :dependencies [[org.clojure/clojure "1.5.1"]                     
                 [net.mikera/core.matrix "0.18.0"]]                
  :source-paths ["dev"])                                           

你好/matric.clj:

(ns hello.matrix                                        
  (:refer-clojure :exclude [* - + == /])                
  (:use [clojure.core.matrix]                           
        [clojure.core.matrix.operators]                 
        [clojure.pprint]))                              

(def x (matrix [[1 2] [3 4]]))                          
(def y (matrix [[5 6] [7 8]]))                          
(def xy (matrix [x y]))                                 

(pprint (slices xy 0))                                  
(pprint (slices xy 1))                                  
(pprint (slices xy 2))  
于 2013-12-26T20:27:17.870 回答