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

Hi,
I have an array “s_norm” with 120 entrys. For every entry a neural network should give me the values for these states
grafik
so as an output i have a matrix with 3 values for every s_norm (a 120x3 matrix)
I also have another array “a”, which is also 120 entrys long and has entrys between one and 3.
Now I want to produce an array, which is also 120 entrys long, but at every entry I want to have the values at position a for every s_norm.
I tried this:
grafik
but that gives me only the values of the firs column at the position a

Can you help me to get for every 120 columns the matching entry a?
probably something with squeeze to remove the dimensions, right?

That would be really nice! Thanks a lot!

You mean,

 [q[i,a[i]] for i=1:120]
1 Like

For the second part, the following would work:

using Random

q = randn(12, 3)  # shortened compared to your case
a = [rand(1:3) for i in 1:size(q, 1)]

x = getindex.(eachrow(q), a)

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