Lyn*_*ite 8 numpy slice multidimensional-array julia
在NumPy中,省略号语法适用于
填充数量,
:直到切片说明符的数量与数组的维度匹配.
(解释这个答案).
我怎么能在朱莉娅那样做?
还没有,但如果你愿意,你可以帮助自己.
import Base.getindex, Base.setindex!
const .. = Val{:...}
setindex!{T}(A::AbstractArray{T,1}, x, ::Type{Val{:...}}, n) = A[n] = x
setindex!{T}(A::AbstractArray{T,2}, x, ::Type{Val{:...}}, n) = A[ :, n] = x
setindex!{T}(A::AbstractArray{T,3}, x, ::Type{Val{:...}}, n) = A[ :, :, n] =x
getindex{T}(A::AbstractArray{T,1}, ::Type{Val{:...}}, n) = A[n]
getindex{T}(A::AbstractArray{T,2}, ::Type{Val{:...}}, n) = A[ :, n]
getindex{T}(A::AbstractArray{T,3}, ::Type{Val{:...}}, n) = A[ :, :, n]
Run Code Online (Sandbox Code Playgroud)
然后你就可以写了
> rand(3,3,3)[.., 1]
3x3 Array{Float64,2}:
0.0750793 0.490528 0.273044
0.470398 0.461376 0.01372
0.311559 0.879684 0.531157
Run Code Online (Sandbox Code Playgroud)
如果您想要更精细的切片,则需要生成/扩展定义或使用分阶段函数.
编辑:现在,请参阅https://github.com/ChrisRackauckas/EllipsisNotation.jl
他们的方法是EllipsisNotation.jl,它增加..了语言。
例子:
julia> using EllipsisNotation
julia> x = rand(1,2,3,4,5);
julia> x[..,3] == x[:,:,:,:,3]
true
julia> x[1,..] == x[1,:,:,:,:]
true
julia> x[1,1,..] == x[1,1,:,:,:]
true
Run Code Online (Sandbox Code Playgroud)
(@mschauer 已经注意到了他的答案(编辑),但参考文献在最后,我觉得这个问题值得一个干净的最新答案。)