Is there a Julia equivalent for the NumPy ellipsis slicing syntax (...)?

In NumPy, the ellipse syntax is this answer ).

How can I do this in Julia?

+6
source share
1 answer

Not yet, but you can help yourself if you want.

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] 

Then you can write

  > 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 

If you need more complex slicing, you need to generate / expand the definition or use stepwise functions.

+3
source

Source: https://habr.com/ru/post/986941/


All Articles