# Assign variables as array of matrices in JuMP

**URL:** <https://discourse.julialang.org/t/assign-variables-as-array-of-matrices-in-jump/43884>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, array\
**Created:** [July 29, 2020, 2:21pm UTC](https://discourse.julialang.org/t/assign-variables-as-array-of-matrices-in-jump/43884 "2020-07-29T14:21:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![umbe](https://avatars.discourse-cdn.com/v4/letter/u/f19dbf/32.png) [@umbe](https://discourse.julialang.org/u/umbe)\
**Post date:** [July 29, 2020, 2:21pm UTC](https://discourse.julialang.org/t/assign-variables-as-array-of-matrices-in-jump/43884/1 "2020-07-29T14:21:11Z")

</div>

Hello to everyone!

I have a question concernig a possible way to provide the problem variables in JuMP.  
I would like to assign the variables in a 3-dimensional matrix, but one of the three dimensions is not constant. To clarify the concept, I add a simplified code that shows my case study.

```julia
using JuMP
using CPLEX

mod = Model(CPLEX.Optimizer)
@variable(mod, x[1:4, i=1:6, 1:i])

```

Now, JuMP accepts the variables written with the form above, but the resulting type of x is: JuMP.Containers.SparseAxisArray{VariableRef,3,Tuple{Int64,Int64,Int64}}. However, being a container, it is not possible to call x with a range in the index (x[1, :, 1] or x[1, 1:6, 1] return both errors). The easiest way that I know to call the variables in this context is exploiting the for:

```julia
[x[1, i, 1] for i=1:6]

```

but, clearly, this form is less compact with respect to the one used for the arrays (x[1, 1:6, 1]) and, in my opinion, makes the script more confusing when it is necessary to write several operations between arrays.

Now, I would ask if it is possible to assign the variables not as a container, but as an array of matrices. In this way, it would be possible to exploit the range in the index to call the variables:

```julia
x[1][:, 1]

```

I hope that I have been sufficiently clear, thank you in advance for your support.  
Bye!

---

<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:** [July 29, 2020, 3:11pm UTC](https://discourse.julialang.org/t/assign-variables-as-array-of-matrices-in-jump/43884/2 "2020-07-29T15:11:52Z")

</div>

```nohighlight
[x[1, i, 1] for i=1:6]

```

This is the recommended way.

As you found out, slicing `SparseAxisArray`s has some issues.

You don’t need to use JuMP’s arrays. You can always do something like:

```julia
model = Model()
x = [@variable(model) for i = 1:3, j = 2:3]

```

---

<div class="post-metadata">

**Author:** ![umbe](https://avatars.discourse-cdn.com/v4/letter/u/f19dbf/32.png) [@umbe](https://discourse.julialang.org/u/umbe)\
**Post date:** [July 29, 2020, 4:41pm UTC](https://discourse.julialang.org/t/assign-variables-as-array-of-matrices-in-jump/43884/3 "2020-07-29T16:41:00Z")

</div>

Hi odow!

Thank you for your answer. I’m very sorry but I do not think to have completely understood your suggestion.  
What would be the advantages in defining the variables as you proposed? Does this alternative allow to assign the variables as an array of matrices?

---

<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:** [July 29, 2020, 4:55pm UTC](https://discourse.julialang.org/t/assign-variables-as-array-of-matrices-in-jump/43884/4 "2020-07-29T16:55:53Z")

</div>

Yes, you could go

```nohighlight
x = [@variable(model, [1:2, 1:2]) for i = 1:3]

```

---

<div class="post-metadata">

**Author:** ![umbe](https://avatars.discourse-cdn.com/v4/letter/u/f19dbf/32.png) [@umbe](https://discourse.julialang.org/u/umbe)\
**Post date:** [July 30, 2020, 7:26am UTC](https://discourse.julialang.org/t/assign-variables-as-array-of-matrices-in-jump/43884/5 "2020-07-30T07:26:16Z")

</div>

Ok, I tried and it perfectly works.  
Thank you very much!
