# Putting results of diffrent arrays for one variable in one array

**URL:** <https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708>\
**Category:** General Usage\
**Created:** [December 26, 2019, 7:14am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708 "2019-12-26T07:14:06Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [December 26, 2019, 7:14am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/1 "2019-12-26T07:14:06Z")

</div>

Basically i have this small piece of code when i get:

```julia
function scan_maker(A)
    m = JuMP.Model(solver=ClpSolver(PrimalTolerance=1e-3, DualTolerance=1e-3, InfeasibleReturn=1, PresolveType=1))
    # m = Model(solver=GurobiSolver())
    level = size(A, 2)
    v = zeros(Int, level)
    ub = zeros(Int, level)
    lb = zeros(Int, level)

    @variable(m, x[1:level])
    @constraint(m, con, A*x.>=0)

    function setc(c)
        for i = 1:size(A, 1)
            m.linconstr[i].lb = float(c[i])
        end
    end
    
    function scan()
        i = 1
        init = 1
        while i > 0
            if i >= init
                @objective(m, Max, x[i])
                res = JuMP.solve(m, suppress_warnings=true)
                if res==:Optimal || res==:Unbounded
                    ub[i] = round(Int, getvalue(x[i]))
                    setobjectivesense(m, :Min)
                    res = JuMP.solve(m, suppress_warnings=true)
                    @assert res==:Optimal || res==:Unbounded
                    lb[i] = round(Int, getvalue(x[i]))

                    v[i] = lb[i]
                    init += 1
                else
                    @assert res==:Infeasible
                    i -= 1
                    continue
                end
            elseif v[i] < ub[i]
                v[i] += 1
            else
                setupperbound(x[i], Inf)
                setlowerbound(x[i], -Inf)
                init -= 1
                i -= 1
                continue
            end

            if i >= level
                vs = []
                for v in vs
                    push!(vs,v)
                end
                    
                @show(vs)
                continue
                
            else
                setupperbound(x[i], v[i])
                setlowerbound(x[i], v[i])
                i += 1
            end
        end
        
    end
    
    return setc, scan
end

```

i get this results :

```julia
vs = [[-1, 5]]
vs = [[0, 3]]
vs = [1, 1]]
vs = [[1, 2]]

```

now i want to get them as one array of multiple arrays as elements, something like this:  
`vs=[[-1,5], [0, 3],[1, 1],[1, 2]]`  
i’m facing problems to get that

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 26, 2019, 7:27am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/2 "2019-12-26T07:27:28Z")

</div>

you can have an empty `vs=[]` outside and then inside the loop: ` push!(vs, v)`

but if you can show more of your code there may be an easier way.

---

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [December 26, 2019, 7:29am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/3 "2019-12-26T07:29:42Z")

</div>

in this case i get :

```julia
vs = [[-1, 5]]
vs = [[0, 3]]
vs = [1, 1]]
vs = [[1, 2]]

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 26, 2019, 7:31am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/4 "2019-12-26T07:31:24Z")

</div>

```julia
julia> vs = []
0-element Array{Any,1}

julia> for i = 1:10
           if rand()>0.5
               v = rand(2)
               push!(vs, v)
           end
       end

julia> vs
6-element Array{Any,1}:
 [0.14439881432341228, 0.22161669979901455]
 [0.09006815010462144, 0.6563286253310212]
 [0.4658664346920076, 0.3135735783668123]
 [0.8744194417513451, 0.9958832473845809]
 [0.8495821037715046, 0.45366928857817346]
 [0.8439848284221538, 0.9645444501222658]

```

I assume your mistake is that you’re remaking `vs=[]` empty every loop

---

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [December 26, 2019, 7:40am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/5 "2019-12-26T07:40:41Z")

</div>

when i do this :

```nohighlight
                vs = []
                for i=1:length(v)
                    push!(vs,v)
                end
                    
                @show(vs)

```

this is what i get:

```julia
vs = Any[[-1, 5], [-1, 5]]
vs = Any[[0, 3], [0, 3]]
vs = Any[[1, 1], [1, 1]]
vs = Any[[1, 2], [1, 2]] 

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 26, 2019, 7:41am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/6 "2019-12-26T07:41:49Z")

</div>

you’re pushing many of the same `v` into `vs`, can you paste more code? I mean, you saw how my example worked, you just gotta match them

---

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [December 26, 2019, 7:43am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/7 "2019-12-26T07:43:17Z")

</div>

i have no problem of posting more code which i will do by editing the question 🙂

---

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [December 26, 2019, 7:45am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/8 "2019-12-26T07:45:58Z")

</div>

i just edited it

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [December 26, 2019, 8:06am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/9 "2019-12-26T08:06:53Z")

</div>

clearly you’re re-defining `vs = []` every `while i > 0`

and why are you pushing nothing into itself?

```julia
        vs = []
                for v in vs #vs here is empty, as defined in last line
                    push!(vs,v)
                end

```

---

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [December 26, 2019, 8:07am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/10 "2019-12-26T08:07:37Z")

</div>

yes that was inside a `while` loop, isn’t it possible inside `while` loop?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 26, 2019, 10:04am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/11 "2019-12-26T10:04:44Z")

</div>

The kind of loop isn’t important. The general pattern of this construction is

```julia
collection = []
<some loop construction that produces items>
    ...
    push!(collection, item)
    ...
end

```

In this case the loop that produces items is the `while i > 0` on the third line of your `scan` function, so the initialization `vs = []` must happen before that, i.e. at the very start of `scan`. Then inside the `if i >= level` condition you should only do `push!(vs, v)`.

---

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [December 26, 2019, 11:37am UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/14 "2019-12-26T11:37:25Z")

</div>

in the code that i posted even when i put an empty array at the very begining of the function and put show after the loop i get only the last array in the following form:

```julia
vs = Any[[1, 2], [1, 2], [1, 2], [1, 2]]

4-element Array{Any,1}:
 [1, 2]
 [1, 2]
 [1, 2]
 [1, 2]

```

and i’m supposed to get :

```julia
[[-1,5],[0,3],[1,1],[1,2]]

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [December 26, 2019, 1:30pm UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/15 "2019-12-26T13:30:31Z")

</div>

Right, you are repeatedly modifying the same vector. In that case you need to do

```julia
push!(vs, copy(v))

```

to save the contents you had at respective time.

---

<div class="post-metadata">

**Author:** ![marouane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marouane/32/16267_2.png) [@marouane](https://discourse.julialang.org/u/marouane)\
**Post date:** [December 26, 2019, 1:32pm UTC](https://discourse.julialang.org/t/putting-results-of-diffrent-arrays-for-one-variable-in-one-array/32708/16 "2019-12-26T13:32:15Z")

</div>

yeas that’s what i did and it worked, finally!! 🙂
