在Julia中,;可用于创建二维数组.
julia> [1 2; 3 4]
2x2 Array{Int64,2}:
1 2
3 4
Run Code Online (Sandbox Code Playgroud)
是否可以使用类似的语法来创建三维(或更高维)的数组?以下是有效的,但我不确定是否有更清洁,更好的方法.
julia> reshape(collect(1:8), 2, 2, 2)
2x2x2 Array{Int64,3}:
[:, :, 1] =
1 3
2 4
[:, :, 2] =
5 7
6 8
Run Code Online (Sandbox Code Playgroud)
Tas*_*nou 10
我想最干净的手动语法是通过cat命令,例如:
cat(3, [1 2 ;3 4], [5 6 ; 7 8]); % concatenate along the 3rd dimension
Run Code Online (Sandbox Code Playgroud)
我想你想要一个列表理解?当您要创建更复杂的数组时,这将使事情变得更容易。
就像是:
[x+1 for x=1:first, y=1:second, z=1:third]
Run Code Online (Sandbox Code Playgroud)
将给出一个first X second X third由 填充的维数组x+1。
有关更多信息,请参阅http://docs.julialang.org/en/release-0.4/manual/arrays/#compressiveons :)