# Select specific elements from each column of a matrix in julia

**URL:** https://discourse.julialang.org/t/select-specific-elements-from-each-column-of-a-matrix-in-julia/127138
**Category:** New to Julia
**Tags:** indexing, matrices
**Created:** [March 19, 2025, 1:59pm UTC](https://discourse.julialang.org/t/select-specific-elements-from-each-column-of-a-matrix-in-julia/127138 "2025-03-19T13:59:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![alex180500](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex180500/32/44358_2.png) [@alex180500](https://discourse.julialang.org/u/alex180500)
#### Post date: [March 19, 2025, 1:59pm UTC](https://discourse.julialang.org/t/select-specific-elements-from-each-column-of-a-matrix-in-julia/127138/1 "2025-03-19T13:59:40Z")

</div>

Let’s say I have this 16x1000 matrix:

```julia
test_mat = rand(16, 1000)

```

And I have these indices:

```julia
test_indices = rand(1:16, 4, 1000)

```

I want to extract from test\_mat a 4x1000 matrix where the indices of the row are represented by each column of test\_indices (for example, the first column should be test\_mat[test\_indices[:, 1], 1] ).

I can do this with a for loop easily but is there a more clever way using some more intelligent indexing? I cannot figure it out.

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [March 19, 2025, 2:49pm UTC](https://discourse.julialang.org/t/select-specific-elements-from-each-column-of-a-matrix-in-julia/127138/2 "2025-03-19T14:49:48Z")

</div>

I won’t call it pretty, but you could use linear indexing

```julia
# you can overwrite test_indices with lin_indices if you want to save an allocation
lin_indices = test_indices .+ (0:size(test_indices,2)-1)' .* size(test_mat,1)
test_mat[lin_indices]

```

Arguably more elegant is `CartesianIndex`

```julia
cart_indices = CartesianIndex.(test_indices, axes(test_indices,2)')
test_mat[cart_indices]

```

But I would probably go with a comprehension like

```julia
[test_mat[test_indices[i,j],j] for i in axes(test_indices,1), j in axes(test_indices,2)]

```

or a simple for loop that fills an array.

---

<div class="post-metadata">

### Author: ![alex180500](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex180500/32/44358_2.png) [@alex180500](https://discourse.julialang.org/u/alex180500)
#### Post date: [March 19, 2025, 4:39pm UTC](https://discourse.julialang.org/t/select-specific-elements-from-each-column-of-a-matrix-in-julia/127138/3 "2025-03-19T16:39:27Z")

</div>

amazing, thank you! why do you think the for loop is the best option? Is it more readable to you?

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [March 19, 2025, 4:50pm UTC](https://discourse.julialang.org/t/select-specific-elements-from-each-column-of-a-matrix-in-julia/127138/4 "2025-03-19T16:50:48Z")

</div>

Any of the above are fine (as is a `for` loop). There may be performance differences among them, and some are easier to read than others. But unless you find that the performance difference of this operation is significant (it probably isn’t, in the grand scheme of your computation) I would go for whatever seems most readable to you.

The nice thing about `for` loops is that they have a comparatively low potential for surprise performance pitfalls and are sometimes easier to understand than more elaborate constructs.

In this case, I would probably use the comprehension. But if you don’t need `test_indices` for anything else, you might think about computing them on a per-column basis and indexing `test_mat` then and there, rather than making a big matrix for the indices and then separately indexing the matrix.

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [March 19, 2025, 6:24pm UTC](https://discourse.julialang.org/t/select-specific-elements-from-each-column-of-a-matrix-in-julia/127138/5 "2025-03-19T18:24:16Z")

</div>

Another way is to slice the matrix and the indices, and `stack` the result. Not quite as efficient as the indexing way (although I’m not quite sure why) but quite a literal translation of your description:

```julia
julia> res1 = [test_mat[test_indices[i,j],j] for i in axes(test_indices,1), j in axes(test_indices,2)]; # as above

julia> res2 = stack(eachcol(test_mat), eachcol(test_indices)) do col, ind
         view(col, ind)
       end;

julia> res1 == res2
true

```
