# Indexing an array with an array of indices

**URL:** <https://discourse.julialang.org/t/indexing-an-array-with-an-array-of-indices/104533>\
**Category:** General Usage\
**Tags:** indexing, arrays\
**Created:** [October 3, 2023, 9:43am UTC](https://discourse.julialang.org/t/indexing-an-array-with-an-array-of-indices/104533 "2023-10-03T09:43:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wosined](https://avatars.discourse-cdn.com/v4/letter/w/9fc29f/32.png) [@wosined](https://discourse.julialang.org/u/wosined)\
**Post date:** [October 3, 2023, 9:43am UTC](https://discourse.julialang.org/t/indexing-an-array-with-an-array-of-indices/104533/1 "2023-10-03T09:43:20Z")

</div>

I have a multidimensional array

> A = [0 for x=1:5,y=1:5,z=1:5]

I also have an array of indices

> I = [1,2,3]

I wish to get the element of `A` at the indices determined by `I`. For example I want to retrieve

> A[1,2,3]

How do I do this? Just doing

> A[I]

gives the wrong answer, because the indices in `I` are interpreted as linear indices (I guess).

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [October 3, 2023, 9:49am UTC](https://discourse.julialang.org/t/indexing-an-array-with-an-array-of-indices/104533/2 "2023-10-03T09:49:10Z")

</div>

Hi and welcome to the forum!

> [@wosined](#):
>
> I = [1,2,3]

Try `CartesianIndex`:

```julia
julia> A = [rand(1:100) for x=1:3,y=1:3,z=1:3]
3×3×3 Array{Int64, 3}:
[:, :, 1] =
 26 69 100
 17 28 3
 87 34 10

[:, :, 2] =
 87 85 63
 16 30 59
 59 28 89

[:, :, 3] =
 17 60 34
 56 5 69
 15 44 10

julia> I = (1,2,3)
(1, 2, 3)

julia> A[CartesianIndex(I)]
60

```

And if your indices are given as a vector, like `I=[1,2,3]`, then use `CartesianIndex(I...)`.

---

<div class="post-metadata">

**Author:** ![wosined](https://avatars.discourse-cdn.com/v4/letter/w/9fc29f/32.png) [@wosined](https://discourse.julialang.org/u/wosined)\
**Post date:** [October 3, 2023, 9:53am UTC](https://discourse.julialang.org/t/indexing-an-array-with-an-array-of-indices/104533/3 "2023-10-03T09:53:56Z")

</div>

It works. What does … mean?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [October 3, 2023, 10:09am UTC](https://discourse.julialang.org/t/indexing-an-array-with-an-array-of-indices/104533/4 "2023-10-03T10:09:39Z")

</div>

```julia
help?> ...
search: ... ..

  ...

  The "splat" operator, ..., represents a sequence of arguments. ... can be used in function
  definitions, to indicate that the function accepts an arbitrary number of arguments. ...
  can also be used to apply a function to a sequence of arguments.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> add(xs...) = reduce(+, xs)
  add (generic function with 1 method)
  
  julia> add(1, 2, 3, 4, 5)
  15
  
  julia> add([1, 2, 3]...)
  6
  
  julia> add(7, 1:100..., 1000:1100...)
  111107

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [October 3, 2023, 1:50pm UTC](https://discourse.julialang.org/t/indexing-an-array-with-an-array-of-indices/104533/5 "2023-10-03T13:50:38Z")

</div>

> [@wosined](#):
>
> I also have an array of indices
> 
> > I = [1,2,3]
> 
> I wish to get the element of `A` at the indices determined by `I`. For example I want to retrieve
> 
> > A[1,2,3]

If you want A [1,2,3] you have to write A[1,2,3].

```julia
julia> A=rand(1:10, (4,3,3))
4×3×3 Array{Int64, 3}:
[:, :, 1] =
  5 8 3
  6 10 6
  4 7 5
 10 8 3

[:, :, 2] =
  4 9 3
  4 1 1
 10 6 7
  2 8 5

[:, :, 3] =
 10 4 10
  3 8 9
  8 4 10
  3 5 8

julia> A[1, 2 ,3]
4

```

If you have I=[1,2,3], one way to write it is A[I…],

You already know how the … works

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [October 3, 2023, 2:21pm UTC](https://discourse.julialang.org/t/indexing-an-array-with-an-array-of-indices/104533/6 "2023-10-03T14:21:38Z")

</div>

`A[I]` calls `getindex(A, I)` under the hood. There are two methods of `getindex` that will give you the result you want.

1. `getindex(A::Array, I::CartesianIndex)`
2. `getindex(A::Array, i1::Integer, i2::Integer, i3::Integer)`

```julia
julia> A = collect(reshape(1:5*4*3, (5,4,3)))
5×4×3 Array{Int64, 3}:
[:, :, 1] =
 1 6 11 16
 2 7 12 17
 3 8 13 18
 4 9 14 19
 5 10 15 20

[:, :, 2] =
 21 26 31 36
 22 27 32 37
 23 28 33 38
 24 29 34 39
 25 30 35 40

[:, :, 3] =
 41 46 51 56
 42 47 52 57
 43 48 53 58
 44 49 54 59
 45 50 55 60

julia> I = CartesianIndex(1,2,3)
CartesianIndex(1, 2, 3)

julia> A[I]
46

julia> A[1,2,3]
46

```

The answers above are showing you how to convert your `I::Vector` into one of the other forms by splatting `...` it into either the `CartesianIndex` constructor or directly into the `getindex` function call.

[Relevant Documentation](https://docs.julialang.org/en/v1/manual/arrays/#man-supported-index-types)
