# Nested loop in @mtkmodel / Matrix manipulation

**URL:** <https://discourse.julialang.org/t/nested-loop-in-mtkmodel-matrix-manipulation/130133>\
**Category:** Modelling & Simulations\
**Tags:** question, for-loop, mtk, mtk-connect, controlsystemsmtk\
**Created:** [June 23, 2025, 1:48pm UTC](https://discourse.julialang.org/t/nested-loop-in-mtkmodel-matrix-manipulation/130133 "2025-06-23T13:48:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![madtorresga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/madtorresga/32/216920_2.png) [@madtorresga](https://discourse.julialang.org/u/madtorresga)\
**Post date:** [June 23, 2025, 1:48pm UTC](https://discourse.julialang.org/t/nested-loop-in-mtkmodel-matrix-manipulation/130133/1 "2025-06-23T13:48:43Z")

</div>

Hi 🙂  
I’m trying to automate the battery cells’ creation from an already defined ECM model:

```julia
const Qs = [4.8 4.4 4.2 ; 3.7 3.5 3.3]
@mtkmodel Series_Connection begin
    @variables begin 
        v_max
        v_min
    end
    @components begin
        cells = [ECM(focv=focv, Q=Qs[i]) for i in eachindex(Qs)]       
        #...
    end
    @equations begin
        # Series connect all cells
        [connect(cells[j+(col*i)].n, cells[j+1+(col*i)].p) for i in 0:row-1, j in 1:col-1] #error

        #Connect in parallel
        connect(source.n, voltage.n, cells[1+col*0].p, cells[1+col*1].p)
        connect(source.p, voltage.p, cells[col*1].n, cells[end].n, ground.g)

        v_max ~ maximum([cells[i].v for i in eachindex(Qs)])
        v_min ~ minimum([cells[i].v for i in eachindex(Qs)])
        #...
    end
end

```

With `ERROR: MethodError: Cannot `convert` an object of type Matrix{Equation} to an object of type Union{Equation, Vector{Equation}}`

How could I implement this otherwise? Even better would be to be able to manipulate matrices instead of arrays.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 23, 2025, 2:13pm UTC](https://discourse.julialang.org/t/nested-loop-in-mtkmodel-matrix-manipulation/130133/2 "2025-06-23T14:13:42Z")

</div>

This might be easier to do without the macro.

---

<div class="post-metadata">

**Author:** ![sass-jacob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sass-jacob/32/215582_2.png) [@sass-jacob](https://discourse.julialang.org/u/sass-jacob)\
**Post date:** [June 23, 2025, 4:56pm UTC](https://discourse.julialang.org/t/nested-loop-in-mtkmodel-matrix-manipulation/130133/3 "2025-06-23T16:56:09Z")

</div>

As an example, you may put:

```julia
eqs_total = Equation[]

eqs = ...
# for Matrix{Equation}, set eqs = reduce(vcat, Symbolics.scalarize(eqs))
# for Vector{Equation}, set eqs = Symbolics.scalarize(eqs)
append!(eqs_total, eqs)

sys = System(eqs_total, ...rest of info)

```
