How to index array at multiple locations

Say I have a n-by-n array and I want to index at a bunch of places

If I was in numpy, I would do something like A[[1,1], [2, 5], [92, 2], [4, 10]

But this doesn’t work in Julia. Is there a way to do this??

One way is to use CartesianIndex

julia> a = rand(3,3)
3×3 Matrix{Float64}:
 0.882002  0.04332   0.477523
 0.829209  0.425031  0.0362383
 0.303801  0.161715  0.422988

julia> a[[CartesianIndex(1,1), CartesianIndex(3,3)]]
2-element Vector{Float64}:
 0.8820020685738753
 0.4229883416619057
1 Like

ah cool! and it works with @views. thanks!

1 Like