# How to loop over vector with varying inner-dimensions?

**URL:** https://discourse.julialang.org/t/how-to-loop-over-vector-with-varying-inner-dimensions/94517
**Category:** General Usage
**Created:** [February 12, 2023, 3:39pm UTC](https://discourse.julialang.org/t/how-to-loop-over-vector-with-varying-inner-dimensions/94517 "2023-02-12T15:39:31Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [February 12, 2023, 3:39pm UTC](https://discourse.julialang.org/t/how-to-loop-over-vector-with-varying-inner-dimensions/94517/1 "2023-02-12T15:39:31Z")

</div>

Hello!

Suppose I have:

```julia
Values = SVector{3, Int64}[[3, 0, 1], [2, 3, 4], [3, 4, 3], [2, 3, 2], [0, 2, 2], [4, 2, 4], [4, 2, 4], [0, 3, 3], [4, 2, 0], [2, 2, 0], [0, 0, 0], [0, 0, 2], [2, 1, 4], [2, 0, 4], [1, 2, 2], [1, 1, 0], [0, 1, 2], [1, 3, 4], [1, 4, 4], [4, 0, 1]]

```

Which a Vector of SVectors with a dimension of 3. I can loop over this as such:

```julia
for (i,j,k) in Values
# do something
end

```

Suppose now I have:

```julia
Values = SVector{2, Int64}[[3, 0], [2, 3], [3, 4], [2, 3], [0, 2], [4, 2], [4, 2], [0, 3], [4, 2], [2, 2], [0, 0], [0, 0], [2, 1], [2, 0,], [1, 2], [1, 1], [0, 1], [1, 3], [1, 4], [4, 0]]

```

Now looping with the previous loop, with (i,j,k) will fail. I know I can change it to (i,j). My question is instead;

Is there an elegant way to avoid switching amount of indices by a “manual” decision and instead let code handle it?

Other than “if dim = 2, (i,j) if dim = 3, (i,j,k)”

Kind regards

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 12, 2023, 3:42pm UTC](https://discourse.julialang.org/t/how-to-loop-over-vector-with-varying-inner-dimensions/94517/2 "2023-02-12T15:42:54Z")

</div>

that doesn’t make sense, what are you gonna do with `i,j,k` anyway if you don’t know how many of them (or even what variable names they will take on) ahead of time?

you can do a double loop maybe:

```julia
for element in Values
    for i in element

```

I suggest you also show what you do with the values later in the function

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [February 12, 2023, 3:48pm UTC](https://discourse.julialang.org/t/how-to-loop-over-vector-with-varying-inner-dimensions/94517/3 "2023-02-12T15:48:51Z")

</div>

It is because I need it to index into a matrix, which can at times be dimensions = 2 or 3.

Perhaps I should find a way to make it into a single vector with some kind of linear indexing then

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [February 12, 2023, 3:49pm UTC](https://discourse.julialang.org/t/how-to-loop-over-vector-with-varying-inner-dimensions/94517/4 "2023-02-12T15:49:28Z")

</div>

> [@Ahmed\_Salih](#):
>
> It is because I need it to index into a matrix, which can at times be dimensions = 2 or 3.

you can index a matrix by splitting the SVector:

```julia
julia> m = rand(2,2)
2×2 Matrix{Float64}:
 0.213414 0.272632
 0.48035 0.538782

julia> a = [1,2];

julia> m[a...]
0.2726319412873929

```

or use `CartesianIndex`, you can directly index a matrix with that.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [February 12, 2023, 5:21pm UTC](https://discourse.julialang.org/t/how-to-loop-over-vector-with-varying-inner-dimensions/94517/5 "2023-02-12T17:21:45Z")

</div>

> [@Ahmed\_Salih](#):
>
> ```julia
> Values = SVector{3, Int64}[[3, 0, 1], [2, 3, 4], [3, 4, 3], [2, 3, 2], [0, 2, 2], [4, 2, 4], [4, 2, 4], [0, 3, 3], [4, 2, 0], [2, 2, 0], [0, 0, 0], [0, 0, 2], [2, 1, 4], [2, 0, 4], [1, 2, 2], [1, 1, 0], [0, 1, 2], [1, 3, 4], [1, 4, 4], [4, 0, 1]]
> 
> ```

You don’t need SVectors for this. Just use either CartesianIndex or tuples:

```julia
vals = [(3,0,1), (2,3,4), (3,4,3), ...]

```

And then use splatting, as mentioned:

```julia
for ind in vals
    M[ind...] = 
end

```

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [February 12, 2023, 5:52pm UTC](https://discourse.julialang.org/t/how-to-loop-over-vector-with-varying-inner-dimensions/94517/6 "2023-02-12T17:52:08Z")

</div>

Don’t know who to mark as solution, thank you both guys!

@jling made me realize I was being stupid and should just index directly

@DNF made me realize that using StaticArrays for this kind of indexing thing is annoying, because one loses the ability to easily increment/decrement index - tuples are better here. StaticArrays shines very much in other parts of my code though 🙂

Kind regards
