I created an issue regarding end
keyword in creating CartesianIndex
in Julia repository a while ago (end keyword in creating CartesianIndex · Issue #33608 · JuliaLang/julia · GitHub):
It is not possible to create a CartesianIndex
object using end
keyword.
This following code does not work in Julia:
CartesianIndex(end,1)
In Python, this is possible using -1
indexing:
import numpy
arr= numpy.array([[4, 2, 3], [2, 5, 4]])
ind=(-1,1) # similar to CartesianIndex
arr[ind]
This is useful for creating algorithms that are independent of the array, such as for sortperm(A, dim)
issue open since 2016
And I got a response from @Keno that:
You can definitely create a type that behaves like this, it just doesn’t have the same semantics as
end
which is defined to looks for the innermost enclosing array (so it does do what you want if you construct the Cartesian index inside the array access). Closing, since this conflicts withend
s semantics, but I would encourage you to try writing the type that behaves like this yourself. It shouldn’t be more than 15 lines of code. If you need help, feel free to open a thread on discourse.
How can I do this?