In NumPy, the ellipsis syntax is for
filling in a number of
:
until the number of slicing specifiers matches the dimension of the array.
(paraphrasing this answer).
How can I do that in Julia?
Array indexing is the same as accessing an array element. You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
Ellipsis is used for slicing multidimensional numpy arrays. The ellipsis syntax may be used to indicate selecting in full any remaining unspecified dimensions.
In Short: Use the Ellipsis as a Placeholder in Python That means you can use an ellipsis as a placeholder similar to the pass keyword. Using three dots creates minimal visual clutter. So, it can be convenient to replace irrelevant code when you're sharing parts of your code online.
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 want more elaborate slicing, you need to generate/expand the definition or use staged functions.
Edit: Nowadays, see https://github.com/ChrisRackauckas/EllipsisNotation.jl
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With