在Julia中将数组合并并按列堆叠

4

我要尝试合并两个一维数组,并将它们在列方向上叠加。

a = [1 2 3]
b = [4 5 6]

# such that, they produce

     a b
c = [1 4
     2 5
     3 6]

# the python syntax for such operation is 
np.stack_column((a,b))

请问有人可以为这个操作提供Julia的语法吗?


3
ab不是1维数组,它们是第一维度为1的2维数组。a=[1,2,3]是1维数组的正确语法。1维数组被视为垂直向量。要堆叠两个1维数组,只需要使用c=[a b]即可。 - 张实唯
1个回答

3

我的一个朋友建议两种方法来执行这个操作:

1.   transpose(vcat(a,b))
     hcat(a', b')
  
2.   reshape(hcat(a,b), (3,2))

两者都会创建以下输出:

 Array{Int64,2}:
 1  4
 2  5
 3  6

3×2的整数数组: 1 4 2 5 3 6


transpose(vcat(a,b)) 是其中最快/最节省内存的方法。 - cj wyett
or shorter [a; b]' - Przemyslaw Szufel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接