# How to batch indexing tensors in Julia

**URL:** <https://discourse.julialang.org/t/how-to-batch-indexing-tensors-in-julia/11671>\
**Category:** General Usage\
**Created:** [June 14, 2018, 9:34am UTC](https://discourse.julialang.org/t/how-to-batch-indexing-tensors-in-julia/11671 "2018-06-14T09:34:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [June 14, 2018, 9:34am UTC](https://discourse.julialang.org/t/how-to-batch-indexing-tensors-in-julia/11671/1 "2018-06-14T09:34:03Z")

</div>

e.g `A = rand(30, 2, 30)` and now I have a batch of index `[1, 2, 1, 1, 1, 2]`, I want to have the result of this batched indexing `res` of size `(30, 6, 30)`, where

`res[:, 1, :] == A[:, 1, :]`

`res[:, 2, :] == A[:, 2, :]`  
`res[:, 3, :] == A[:, 1, :]`  
…

Is there any builtin function that can do this?

---

<div class="post-metadata">

**Author:** ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)\
**Post date:** [June 14, 2018, 9:58am UTC](https://discourse.julialang.org/t/how-to-batch-indexing-tensors-in-julia/11671/2 "2018-06-14T09:58:35Z")

</div>

I think `A[:, [1, 2, 1, 1, 1, 2], :]` could be what you are looking for.
