# Can JuMP.jl create a sparse array variable that can be used as a full matrix?

**URL:** <https://discourse.julialang.org/t/can-jump-jl-create-a-sparse-array-variable-that-can-be-used-as-a-full-matrix/69114>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [October 2, 2021, 2:15pm UTC](https://discourse.julialang.org/t/can-jump-jl-create-a-sparse-array-variable-that-can-be-used-as-a-full-matrix/69114 "2021-10-02T14:15:31Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [October 2, 2021, 3:35pm UTC](https://discourse.julialang.org/t/can-jump-jl-create-a-sparse-array-variable-that-can-be-used-as-a-full-matrix/69114/2 "2021-10-02T15:35:59Z")

</div>

One option would be as follows:

```julia
julia> using JuMP

julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> x = zeros(AffExpr, 3, 3)
3×3 Matrix{AffExpr}:
 0 0 0
 0 0 0
 0 0 0

julia> x[1, 1] = @variable(model, base_name = "x[1, 1]")
x[1, 1]

julia> x[2, 2] = @variable(model, base_name = "x[2, 2]")
x[2, 2]

julia> x[1, 3] = @variable(model, base_name = "x[1, 3]")
x[1, 3]

julia> x[3, 1] = @variable(model, base_name = "x[3, 1]")
x[3, 1]

julia> x[3, 3] = @variable(model, base_name = "x[3, 3]")
x[3, 3]

julia> x
3×3 Matrix{AffExpr}:
 x[1, 1] 0 x[1, 3]
 0 x[2, 2] 0
 x[3, 1] 0 x[3, 3]

```

Another option is as follows

```nohighlight
julia> using SparseArrays

julia> s = sparse([1, 3, 2, 1, 3], [1, 1, 2, 3, 3], @variable(model, y[1:5]))
3×3 SparseMatrixCSC{VariableRef, Int64} with 5 stored entries:
 y[1] ⋅ y[4]
 ⋅ y[3] ⋅
 y[2] ⋅ y[5]

julia> Matrix(s)
3×3 Matrix{AffExpr}:
 y[1] 0 y[4]
 0 y[3] 0
 y[2] 0 y[5]

```

---

_[View the full topic](https://discourse.julialang.org/t/can-jump-jl-create-a-sparse-array-variable-that-can-be-used-as-a-full-matrix/69114)._
