# 3D variable in JuMP

I am working on a convex optimization problem that needs to define several (e.g. 5) symmetric matrices as variables. I was wondering if it is possible to define those variables with a single line like `@variable(m, x(N,N,M), Symmetric)`, where N is the dimension of the matrix, and M is the number of copies of symmetric matrices. Then I can access a symmetric matrix by, e.g., `x[:,:,1]`.

From this post (http://ask.cvxr.com/t/semidefinite-relaxations-of-quadratic-constraint/179), it seems to be possible in CVX. So I was thinking it would be nice if I could do similar things in JuMP.

This is not yet possible in JuMP, you need to do

``````x = Array{JuMP.VariableRef}(undef, N, N, M)
for i in 1:M
x[:, :, i] = @variable(m, [1:N, 1:N], Symmetric)
end
``````

or do

``````x = [@variable(m, [1:N, 1:N], Symmetric) for i in 1:M]
``````

Thank you for the promp reply!

The second code works for me. I also have two tips for implementing this as a future reference:

1. In order to make the model more readable, I set the names of the matrices as follows: `x = [@variable(m, [1:N, 1:N], base_name = "x\$i", Symmetric) for i in 1:M]`. Then in the printed model an item in the first matrix looks like this: `x1[i,j]`.

2. To access the ith matrix, use `x[i][:,:]`

For the first option though, I get a bug from `x = Matrix{JuMP.VariableRef}(undef, 2, 2, 3)`, which says `MethodError: no method matching Array{VariableRef,2}(::UndefInitializer, ::Int64, ::Int64, ::Int64)`. I assume this is because JuMP does not support 3-dimensional variables.

Oops, `Matrix` should be replaced by `Array` in the first code.

Now it works. In this case the ith matrix is accessed by `x[:,:,i]`.