Julia中的分割等价

Ali*_*Ali 11 arrays wolfram-mathematica julia

Mathematica Partition在Julia中的功能是什么?

Mathematica Partition[list,n]采用一个数组并将其分成不重叠的长度子列表n.另一方面,Julia中的分区函数接受一个数组,并将该数组的所有分区都放入n子集中.

Iai*_*ing 11

所以在Mathematica中:

In[1]:= Partition[{a, b, c, d, e, f}, 2]
Out[1]= {{a,b},{c,d},{e,f}}
Run Code Online (Sandbox Code Playgroud)

但在朱莉娅,这个partitions功能有着截然不同的含义:

x = [:a,:b,:c,:d,:e,:f]
first(partitions(x,2))
#2-element Array{Array{Symbol,1},1}:
# [:a,:b,:c,:d,:e]
# [:f]
Run Code Online (Sandbox Code Playgroud)

它是集合中所有2个分区的集合.为了得到你想要的东西,你可以做点什么

yourpart(x,n) = {{x[i:min(i+n-1,length(x))]} for i in 1:n:length(x)}
Run Code Online (Sandbox Code Playgroud)

julia> yourpart([:a,:b,:c,:d,:e,:f], 2)
3-element Array{Any,1}:
 {:a,:b}
 {:c,:d}
 {:e,:f}

julia> yourpart(x,4)
2-element Array{Any,1}:
 {[:a,:b,:c,:d]}
 {[:e,:f]}
Run Code Online (Sandbox Code Playgroud)

  • 为了避免弃用警告(从Julia 0.4.5开始),你可以将其写为"你的部分(x,n)= [x [i:min(i + n-1,length(x))] i in 1: N:长度(X)]` (2认同)

cd9*_*d98 9

也许我错过了什么,但这不是你想做的吗?

x = [:a,:b,:c,:d,:e,:f]
n = 2
reshape(x, (n, div(length(x), n)))
Run Code Online (Sandbox Code Playgroud)