# Acess an array using another array

**URL:** https://discourse.julialang.org/t/acess-an-array-using-another-array/73714
**Category:** General Usage
**Tags:** question, array
**Created:** [December 28, 2021, 4:36pm UTC](https://discourse.julialang.org/t/acess-an-array-using-another-array/73714 "2021-12-28T16:36:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Renan\_Rabelo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/renan_rabelo/32/8048_2.png) [@Renan\_Rabelo](https://discourse.julialang.org/u/Renan_Rabelo)
#### Post date: [December 28, 2021, 4:36pm UTC](https://discourse.julialang.org/t/acess-an-array-using-another-array/73714/1 "2021-12-28T16:36:22Z")

</div>

I want to be able to acces a multi-dimentional array using another array. This is what happens if I try to do it:

```julia
x = [1,1,1]
y = zeros(Int64,(3,3,3)) # 3D array
println(y[x]) # Prints [0,0,0]
println(y[1,1,1]) # Prints 0

```

`println(y[x])` isn’t printing `0`, why?

Thanks!

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [December 28, 2021, 4:39pm UTC](https://discourse.julialang.org/t/acess-an-array-using-another-array/73714/2 "2021-12-28T16:39:59Z")

</div>

With `x = [1,1,1]` writing `y[x]` is the same as writing `y[[1,1,1]]`. This takes 3 times the same value from `y`: it considers the 3D array as a 1D array and takes the first value (three times). On the other hand, writing `y[1,1,1]` takes one time the value at position (1,1,1) (which is also the first value).

To do the equivalent of `y[1,1,1]` with `x` as index you can use the _splatting_ operator `...` as in `y[x...]`.

---

<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: [December 28, 2021, 6:10pm UTC](https://discourse.julialang.org/t/acess-an-array-using-another-array/73714/3 "2021-12-28T18:10:23Z")

</div>

Julia has a dedicated type for multidimensional indices. It will behave the way you’re hoping.

```julia
x = CartesianIndex(1,1,1)
println(y[x])

```

`CartesianIndex` behaves like a single index (rather than array), which is why it returns a single value. This is preferred (syntactically, stylistically, and performance-wise) to splatting arrays as indices (e.g., `y[[1,1,1]...]`).

To elaborate on the previous reply, your initial attempt to index an array `y` with an array `x` that has `N` values will return an array with `N` values, where each entry in `x` selects which entry in `y` appears in those `N` positions. Hence `y[[1,1,1]]` returns the `[y[1],y[1],y[1]]`. When multidimensional arrays are indexed with single indices like `y[1]`, they use “linear indexing”. See `reshape(1:12,3,4)` for an example of what this looks like.

---

<div class="post-metadata">

### Author: ![Renan\_Rabelo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/renan_rabelo/32/8048_2.png) [@Renan\_Rabelo](https://discourse.julialang.org/u/Renan_Rabelo)
#### Post date: [December 28, 2021, 6:25pm UTC](https://discourse.julialang.org/t/acess-an-array-using-another-array/73714/4 "2021-12-28T18:25:37Z")

</div>

Thanks!

Splatting alocates a lot of memory, your solution is more performant.
