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

**URL:** <https://discourse.julialang.org/t/how-to-produce-an-array-with-specific-entries-of-a-matrix/83359>\
**Category:** New to Julia\
**Tags:** question, arrays\
**Created:** [June 26, 2022, 2:01pm UTC](https://discourse.julialang.org/t/how-to-produce-an-array-with-specific-entries-of-a-matrix/83359 "2022-06-26T14:01:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hupdup](https://avatars.discourse-cdn.com/v4/letter/h/7ba0ec/32.png) [@Hupdup](https://discourse.julialang.org/u/Hupdup)\
**Post date:** [June 26, 2022, 2:01pm UTC](https://discourse.julialang.org/t/how-to-produce-an-array-with-specific-entries-of-a-matrix/83359/1 "2022-06-26T14:01:24Z")

</div>

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](https://global.discourse-cdn.com/julialang/original/3X/e/6/e6ebe81c2a793d417fbb1e6a71ec119173a7a696.png)  
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](https://global.discourse-cdn.com/julialang/original/3X/1/7/17c4e3f3aa281b32808535ac544d3829f40beec1.png)  
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!

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [June 26, 2022, 2:37pm UTC](https://discourse.julialang.org/t/how-to-produce-an-array-with-specific-entries-of-a-matrix/83359/2 "2022-06-26T14:37:05Z")

</div>

You mean,

```julia
 [q[i,a[i]] for i=1:120]

```

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [June 26, 2022, 2:38pm UTC](https://discourse.julialang.org/t/how-to-produce-an-array-with-specific-entries-of-a-matrix/83359/3 "2022-06-26T14:38:47Z")

</div>

For the second part, the following would work:

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [June 26, 2022, 4:30pm UTC](https://discourse.julialang.org/t/how-to-produce-an-array-with-specific-entries-of-a-matrix/83359/4 "2022-06-26T16:30:36Z")

</div>

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

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

```

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

```julia
struct ∇
    n:Vector{Int}
end

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

q[∇(a)]

```
