1

假设有两个长度相同的一维数组:

let x = fromListUnboxed (ix1 4) [1, 2, 3, 4]
let y = fromListUnboxed (ix1 4) [5, 6, 7, 8]

现在我想将这两个数组堆叠成一个二维数组,以便这些数组形成行。我怎样才能在repa中做到这一点?

基本上,我正在寻找相当于 numpy 的row_stack

>>> x = np.array([1, 2, 3, 4])
>>> y = np.array([5, 6, 7, 8])
>>> np.row_stack((x, y))
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

笔记。两个数组xy来自外部,即我无法从头开始创建二维数组。

4

1 回答 1

1

正如我在最初的评论中提到的那样,您所需要的就是reshape然后append(都在Data.Array.Repa中。

ghci> let x' = reshape (ix2 4 1) x
ghci> let y' = reshape (ix2 4 1) y
ghci> z <- computeP $ x' `append` y' :: IO (Array U DIM2 Int)
ghci> z
AUnboxed ((Z :. 4) :. 2) [1,5,2,6,3,7,4,8]

至于漂亮的印刷,repa不是很好(可能是因为没有更好的更高尺寸的漂亮印刷)。这是一个要显示的单行hackz

ghci> putStr $ unlines [ unwords [ show $ z ! ix2 i j  |  i<-[0..3] ] | j<-[0..1] ]
1 2 3 4
5 6 7 8
于 2016-06-29T17:28:36.247 回答