# Cannot collect variables with index not starting from 1 in a vector

**URL:** <https://discourse.julialang.org/t/cannot-collect-variables-with-index-not-starting-from-1-in-a-vector/2739>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [March 17, 2017, 6:28pm UTC](https://discourse.julialang.org/t/cannot-collect-variables-with-index-not-starting-from-1-in-a-vector/2739 "2017-03-17T18:28:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wei\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_wang/32/15032_2.png) [@Wei\_Wang](https://discourse.julialang.org/u/Wei_Wang)\
**Post date:** [March 17, 2017, 6:28pm UTC](https://discourse.julialang.org/t/cannot-collect-variables-with-index-not-starting-from-1-in-a-vector/2739/1 "2017-03-17T18:28:58Z")

</div>

Hi,  
I am trying to create anonymous variables and collect them in a vector. But only those with index starting from 1 can be collected. Others will result in error. Is there any method to push those variables into a vector? I paste an example below. Thanks

```julia
using JuMP,Gurobi
I=1:5
m=Model(solver=GurobiSolver())
x=Vector{Variable}[] # the vector used to collec variables
y1=@variable(m,[i=1:4],basename="y1") # variable with index starting from 1
y2=@variable(m,[i=4:6],basename="y2") # variable with index not starting from 1
y3=@variable(m,[i in I],basename="y3") # variable with index in an array
push!(x,y1) # good
push!(x,y2) # error
push!(x,y3) # error

```

The error reported is

```julia
MethodError: Cannot `convert` an object of type JuMP.JuMPArray{JuMP.Variable,1,Tuple{UnitRange{Int64}}} to an object of type Array{JuMP.Variable,1}
This may have arisen from a call to the constructor Array{JuMP.Variable,1}(...),
since type constructors fall back to convert methods.

 in push!(::Array{Array{JuMP.Variable,1},1}, ::JuMP.JuMPArray{JuMP.Variable,1,Tuple{UnitRange{Int64}}}) at .\array.jl:479

```

---

<div class="post-metadata">

**Author:** ![joehuchette](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joehuchette/32/32_2.png) [@joehuchette](https://discourse.julialang.org/u/joehuchette)\
**Post date:** [March 18, 2017, 5:51pm UTC](https://discourse.julialang.org/t/cannot-collect-variables-with-index-not-starting-from-1-in-a-vector/2739/2 "2017-03-18T17:51:42Z")

</div>

This is because the types of `y2` and `y3` are not `Vector{Variable}`, and you are trying to push them to a typed array `x` where each element must be of type `Vector{Variable}`.

---

<div class="post-metadata">

**Author:** ![Wei\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_wang/32/15032_2.png) [@Wei\_Wang](https://discourse.julialang.org/u/Wei_Wang)\
**Post date:** [March 20, 2017, 3:19pm UTC](https://discourse.julialang.org/t/cannot-collect-variables-with-index-not-starting-from-1-in-a-vector/2739/3 "2017-03-20T15:19:41Z")

</div>

Yes, I can tell that. But it is weird to me that y2 and y3 are in different type just because the way I define their index.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [March 21, 2017, 4:54am UTC](https://discourse.julialang.org/t/cannot-collect-variables-with-index-not-starting-from-1-in-a-vector/2739/4 "2017-03-21T04:54:49Z")

</div>

What @joehuchette means is that your array is declared wrong. It should be `Variable[]` not `Vector{Variable}[]`. I strongly suspect the error is actually on the first push.

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [March 22, 2017, 5:31pm UTC](https://discourse.julialang.org/t/cannot-collect-variables-with-index-not-starting-from-1-in-a-vector/2739/5 "2017-03-22T17:31:19Z")

</div>

It’s weird, but we support things like `@variable(m,[i=1:N,j=1:N])` which returns a plain matrix, and `@variable(m,[i=1:N,j=i:N])` which returns a specialized JuMPDict. If you have suggestions on improvements for the syntax, I’m all ears. The solution to your code snippet is to define `x = []`.

---

<div class="post-metadata">

**Author:** ![Wei\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_wang/32/15032_2.png) [@Wei\_Wang](https://discourse.julialang.org/u/Wei_Wang)\
**Post date:** [March 22, 2017, 8:51pm UTC](https://discourse.julialang.org/t/cannot-collect-variables-with-index-not-starting-from-1-in-a-vector/2739/6 "2017-03-22T20:51:09Z")

</div>

The x defined as that works for me. Now I understand why they are in different types. Thank you so much.
