How to produce an array with specific entries of a matrix?

There is a method CartesianIndex, you can use it like this:

q[CartesianIndex.(1:120, a)]

But if I were you, and this indexing is used often, I will define a custom indexing method:

struct ∇
    n:Vector{Int}
end

Base.getindex(ma::Matrix,i::∇)=ma[CartesianIndex.(1:size(ma,1),i.n)]

q[∇(a)]
2 Likes