# JuMP: How to construct a matrix with both variables and real numbers

**URL:** <https://discourse.julialang.org/t/jump-how-to-construct-a-matrix-with-both-variables-and-real-numbers/4127>\
**Category:** Optimization (Mathematical)\
**Created:** [June 7, 2017, 4:44am UTC](https://discourse.julialang.org/t/jump-how-to-construct-a-matrix-with-both-variables-and-real-numbers/4127 "2017-06-07T04:44:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ksun46](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksun46/32/9437_2.png) [@Ksun46](https://discourse.julialang.org/u/Ksun46)\
**Post date:** [June 7, 2017, 4:44am UTC](https://discourse.julialang.org/t/jump-how-to-construct-a-matrix-with-both-variables-and-real-numbers/4127/1 "2017-06-07T04:44:31Z")

</div>

I am a starter to JuMP and I am trying to construct a matrix that consists of both variables and real number(0s). The first way I can think of is to push! entries(variables or 0s) to an array and then push! multiple arrays to another array. But this array of arrays may not work well for matrix multiplication since the dimension doesn’t match. The second way is to construct a matrix of variables @variable(model, C[1:100,1:100]), and then fix some entries to 0. But those entries with fixed values are still considered as variables in the solver, so I am adding unnecessary variables to the model. Therefore, does anyone know how to construct a matrix (2D array, not array of arrays) that consists of both variables and 0s? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [June 7, 2017, 5:51am UTC](https://discourse.julialang.org/t/jump-how-to-construct-a-matrix-with-both-variables-and-real-numbers/4127/2 "2017-06-07T05:51:37Z")

</div>

You could try ` d = Matrix(n, m); `  
which returns a n\*m matrix of #undefined values, it’s a bit of a workaround for the zeros(type ::Any) error

Note that julia doesn’t like to display (vectors of?) #undef’s, eg print(d[1, :]) will probably err until you put data in there

---

<div class="post-metadata">

**Author:** ![Ksun46](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksun46/32/9437_2.png) [@Ksun46](https://discourse.julialang.org/u/Ksun46)\
**Post date:** [June 7, 2017, 6:16am UTC](https://discourse.julialang.org/t/jump-how-to-construct-a-matrix-with-both-variables-and-real-numbers/4127/3 "2017-06-07T06:16:05Z")

</div>

Thanks I will try! I should have got more familiar with Julia before using JuMP.

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [June 7, 2017, 6:54am UTC](https://discourse.julialang.org/t/jump-how-to-construct-a-matrix-with-both-variables-and-real-numbers/4127/4 "2017-06-07T06:54:19Z")

</div>

If your non-variable entries will always take on the value 0, could you not just use a sparse array?
