# Define variables on each elemnts of a vector of vectors

**URL:** <https://discourse.julialang.org/t/define-variables-on-each-elemnts-of-a-vector-of-vectors/81495>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, optimization, modelling\
**Created:** [May 23, 2022, 7:53am UTC](https://discourse.julialang.org/t/define-variables-on-each-elemnts-of-a-vector-of-vectors/81495 "2022-05-23T07:53:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Optimization](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/optimization/32/32462_2.png) [@Optimization](https://discourse.julialang.org/u/Optimization)\
**Post date:** [May 23, 2022, 7:53am UTC](https://discourse.julialang.org/t/define-variables-on-each-elemnts-of-a-vector-of-vectors/81495/1 "2022-05-23T07:53:52Z")

</div>

I’m trying to find a way to define a varible `x_ijk` where `i in I` is an n-elemnts vector of vectors, `j in J` and ` k in K` are both vectors. In my case, always the length of `K` is equal to the number of vectors in `I` .

The question is related on how we can index over each vectors in `I` separatly with an orederd and separate combination with `K`.

For example:

```julia
I = [[2,6,5], [1,2,4,5,9]]
J = [1,2,3]
K = [4,5] # for a better explanation suppose K = [a,b]

```

How to have the variables `x_ijk` indexed with a pair entries in every vector `I` and associated ordered elements in `K`? I mean, the combination of each elemnts of the first vector of `I` and first elements in `K`. Then index over eache elements of the second vector in `I` and the second elements in `K`.

The desired output is like this:

```julia
# for a better explanation suppose K = [a,b]
# for each vector in I and associated elemnts in K having a variable x_ijk where each elemnts of the nth vector in `I` is paired and indexed with nth element in `K`

# for first pair (i.e. I=[2,6,5] ,K = a)
x[2,1,a], x[2,2,a], x[2,3,a], x[6,1,a], .... x[5,3,a] # in other words we cannot have x[2,1,b] or any other combination with `b`

# for second pair ( i.e. I=[1,2,4,5,9] ,K = b)
x[1,1,b], x[1,2,b],..., x[4,1,b], .... x[9,3,b]

```

Is this possible? I’ve tried many thing but neither were succesful? Like the following:

```julia
for idx in 1:length(K)
    @variable(model, x[i in I[idx], j in J, k in K] >= 0, Bin)
end

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [May 23, 2022, 8:50am UTC](https://discourse.julialang.org/t/define-variables-on-each-elemnts-of-a-vector-of-vectors/81495/2 "2022-05-23T08:50:24Z")

</div>

I would just pre-compute the set of indices that you want:

```julia
I = [[2, 6, 5], [1, 2, 4, 5, 9]]
J = [1, 2, 3]
K = [4,v5]
sets = [(i, j, k) for (ki, k) in enumerate(K) for j in J for i in I[ki]]
model = Model()
@variable(model, x[sets])
x[(2, 3, 5)]

```

---

<div class="post-metadata">

**Author:** ![Optimization](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/optimization/32/32462_2.png) [@Optimization](https://discourse.julialang.org/u/Optimization)\
**Post date:** [May 23, 2022, 9:42am UTC](https://discourse.julialang.org/t/define-variables-on-each-elemnts-of-a-vector-of-vectors/81495/3 "2022-05-23T09:42:54Z")

</div>

Thanks @odow dow Very Much!
