# Mix of index-based and slice based indexing across different dimensions

**URL:** <https://discourse.julialang.org/t/mix-of-index-based-and-slice-based-indexing-across-different-dimensions/102603>\
**Category:** General Usage\
**Tags:** arrays\
**Created:** [August 8, 2023, 1:42pm UTC](https://discourse.julialang.org/t/mix-of-index-based-and-slice-based-indexing-across-different-dimensions/102603 "2023-08-08T13:42:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexanderNenninger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexandernenninger/32/24602_2.png) [@AlexanderNenninger](https://discourse.julialang.org/u/AlexanderNenninger)\
**Post date:** [August 8, 2023, 1:42pm UTC](https://discourse.julialang.org/t/mix-of-index-based-and-slice-based-indexing-across-different-dimensions/102603/1 "2023-08-08T13:42:02Z")

</div>

Given an k-dimensional array, e.g.

```julia
A = LinearIndices((2,2,2))
2×2×2 LinearIndices{3, Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}, Base.OneTo{Int64}}}:
[:, :, 1] =
 1 3
 2 4

[:, :, 2] =
 5 7
 6 8

```

and a list of row-column indices, e.g. `idxs=[(1,2), (1,1)]`, how do I efficiently select the corresponding rows and columns, while slicing in the remaining dimensions?

In the given example, the outcome should be

```julia
3 7
1 5

```

i.e. taking the elements `(1,2,1)`, `(1,2,2)`, `(1,1,1)`, and `(1,1,2)`.

We can ignore the exact shape of the output for now, as that can be fixed using `reshape`.

Edit: The command `reduce(hcat, (x -> @view A[x..., :]).(idxs))` solves the problem in principle, but maybe there’s a way to get one single view, instead of a copy.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 8, 2023, 4:00pm UTC](https://discourse.julialang.org/t/mix-of-index-based-and-slice-based-indexing-across-different-dimensions/102603/2 "2023-08-08T16:00:39Z")

</div>

Make those `idxs` an array of `CartesianIndex`:

```julia
julia> idxs = CartesianIndex.([(1,2), (1,1)])
2-element Vector{CartesianIndex{2}}:
 CartesianIndex(1, 2)
 CartesianIndex(1, 1)

julia> A[idxs, :]
2×2 Matrix{Int64}:
 3 7
 1 5

```
