# Indexing array based on a vector including Colon()

**URL:** <https://discourse.julialang.org/t/indexing-array-based-on-a-vector-including-colon/18121>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 29, 2018, 12:49am UTC](https://discourse.julialang.org/t/indexing-array-based-on-a-vector-including-colon/18121 "2018-11-29T00:49:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![YoungFaithful](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngfaithful/32/6237_2.png) [@YoungFaithful](https://discourse.julialang.org/u/YoungFaithful)\
**Post date:** [November 29, 2018, 12:49am UTC](https://discourse.julialang.org/t/indexing-array-based-on-a-vector-including-colon/18121/1 "2018-11-29T00:49:04Z")

</div>

I’m trying to index a multidimensional array based on a vector using Colon() as well. Is there an easy way to reformulate the getindex function call to allow for varying dimensions of A and corresponding lengths of the indexing vector?

```julia
    A=reshape(collect(1:12),(3,2,2))
    index=[:,1,2]
    val=getindex(A,index[1],index[2],index[3])

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [November 29, 2018, 10:19am UTC](https://discourse.julialang.org/t/indexing-array-based-on-a-vector-including-colon/18121/2 "2018-11-29T10:19:52Z")

</div>

[Essentials · The Julia Language](https://docs.julialang.org/en/v1.0/base/base/#)…  
[Functions · The Julia Language](https://docs.julialang.org/en/v1.0/manual/functions/#Varargs-Functions-1)

---

<div class="post-metadata">

**Author:** ![YoungFaithful](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youngfaithful/32/6237_2.png) [@YoungFaithful](https://discourse.julialang.org/u/YoungFaithful)\
**Post date:** [November 29, 2018, 6:18pm UTC](https://discourse.julialang.org/t/indexing-array-based-on-a-vector-including-colon/18121/3 "2018-11-29T18:18:31Z")

</div>

Thanks for your reply. I fear I didn’t express my aim well enough or didn’t understand the solution you pointed to yet:  
I’m trying to pass each field of index to the getindex-function as an argument without writing it out like I do it rn:

```julia
    if length(index_set)==3
        result=getindex(variable.innerArray,index_num[1],index_num[2],index_num[3])
    elseif length(index_set)==5
        result=getindex(variable.innerArray,index_num[1],index_num[2],index_num[3]index_num[4],index_num[5])
    end

```

I understood the solution you provided as an option to write the getindex()-function on a way to accept a varying amount of arguments, correct?

---

<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:** [November 29, 2018, 7:28pm UTC](https://discourse.julialang.org/t/indexing-array-based-on-a-vector-including-colon/18121/4 "2018-11-29T19:28:29Z")

</div>

You can always just splat the index set into indexing syntax:

```julia
julia> A[index...]
3-element Array{Int64,1}:
 7
 8
 9

```

But if this is in a hot loop, you’ll soon realize that this isn’t as fast as it could be since splatting the contents of an array isn’t type-stable: Julia’s inference doesn’t know ahead of time how many elements an array might contain or what types they might be (in this case). Better if you can construct your index set as a Tuple instead: `index = (:, 1, 2)`.

There are lots of great tricks Julia has up its sleeve in constructing fast, tight, and readable indexing loops — check out Tim’s posts on multidimensional iteration and the manual for CartesianIndices.
