假设有两个相同长度的1-D阵列:
let x = fromListUnboxed (ix1 4) [1, 2, 3, 4]
let y = fromListUnboxed (ix1 4) [5, 6, 7, 8]
现在我想将这两个数组堆叠成一个二维数组,以便这些数组形成行。我怎么能在修理中做到这一点?
基本上,我正在寻找相当于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]])
注意。两个数组x
和y
来自外部,即我无法从头开始创建二维数组。
答案 0 :(得分:1)
正如我在初始评论中提到的,您只需要reshape
然后append
(Data.Array.Repa中都有。{/ p>
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
并不是很好(可能因为没有很好的漂亮印刷来获得更高的尺寸)。这是一个显示z
ghci> putStr $ unlines [ unwords [ show $ z ! ix2 i j | i<-[0..3] ] | j<-[0..1] ]
1 2 3 4
5 6 7 8