# ArgumentError: no outer repetition count may be negative; got (-1,)

**URL:** <https://discourse.julialang.org/t/argumenterror-no-outer-repetition-count-may-be-negative-got-1/77384>\
**Category:** General Usage\
**Tags:** arrays, matrices\
**Created:** [March 3, 2022, 11:54pm UTC](https://discourse.julialang.org/t/argumenterror-no-outer-repetition-count-may-be-negative-got-1/77384 "2022-03-03T23:54:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbokser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbokser/32/31188_2.png) [@bbokser](https://discourse.julialang.org/u/bbokser)\
**Post date:** [March 3, 2022, 11:54pm UTC](https://discourse.julialang.org/t/argumenterror-no-outer-repetition-count-may-be-negative-got-1/77384/1 "2022-03-03T23:54:18Z")

</div>

I have received the following error, and could not find any explanation for its meaning in Google search results.

```julia
ArgumentError: no outer repetition count may be negative; got (-1,)

Stacktrace:
 [1] check
   @ ./abstractarraymath.jl:318 [inlined]
 [2] #repeat#1
   @ ./abstractarraymath.jl:267 [inlined]
 [3] repeat
   @ ./abstractarraymath.jl:224 [inlined]
 [4] buildQP_constrained!(ctrl::MPCController{OSQP.Model}, A::SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}, B::SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}, Q::Diagonal{Float64, Vector{Float64}}, R::Diagonal{Float64, Vector{Float64}}, Qf::Matrix{Float64}; kwargs::Base.Iterators.Pairs{Symbol, Real, Tuple{Symbol, Symbol}, NamedTuple{(:tol, :verbose), Tuple{Float64, Bool}}})
   @ Main ./In[33]:36
 [5] #buildQP!#36
   @ ./In[11]:60 [inlined]
 [6] top-level scope
   @ In[34]:7
 [7] eval
   @ ./boot.jl:360 [inlined]
 [8] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1116

```

The code causing this problem is as shown:

```julia
    ABI = hcat(A, B, -I)

    AB = [sparse(hcat(A, B))]
    
    E = blockdiag(sparse(B), repeat(AB, Nh-3)..., sparse(ABI)) # offblockdiag
    
    # Add the identity matrices
    for i = 1:Nh-1
        E[N_r*(i-1).+(1:N_r), N_b2+N_ab2*(i-1).+(1:N_I)] .= -I(N_I)
    end

```

According to the stacktrace, the error is occurring on the following line: `E = blockdiag(sparse(B), repeat(AB, Nh-3)..., sparse(ABI))`

The following information may also be useful:

> typeof(A) = SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}  
> typeof(B) = SubArray{Float64, 2, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, UnitRange{Int64}}, true}

---

<div class="post-metadata">

**Author:** ![bbokser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbokser/32/31188_2.png) [@bbokser](https://discourse.julialang.org/u/bbokser)\
**Post date:** [March 4, 2022, 2:04am UTC](https://discourse.julialang.org/t/argumenterror-no-outer-repetition-count-may-be-negative-got-1/77384/2 "2022-03-04T02:04:33Z")

</div>

I am now quite sure this is a problem with `repeat()`, although I’m not sure whether the mistake is on my end or the API’s.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 4, 2022, 2:11am UTC](https://discourse.julialang.org/t/argumenterror-no-outer-repetition-count-may-be-negative-got-1/77384/3 "2022-03-04T02:11:53Z")

</div>

The `Nh` variable may be smaller than or equal to 3? I would guess it has value `2` when the error occurs. (You would be passing `-1` to repeat in this case.)

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [March 4, 2022, 2:13am UTC](https://discourse.julialang.org/t/argumenterror-no-outer-repetition-count-may-be-negative-got-1/77384/4 "2022-03-04T02:13:18Z")

</div>

Please provide a Minimum Working Example. See point 4 in this post:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![bbokser](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bbokser/32/31188_2.png) [@bbokser](https://discourse.julialang.org/u/bbokser)\
**Post date:** [March 4, 2022, 2:21am UTC](https://discourse.julialang.org/t/argumenterror-no-outer-repetition-count-may-be-negative-got-1/77384/5 "2022-03-04T02:21:25Z")

</div>

Yep, this was the problem. `Nh` was being rewritten from a value of 50 down to 2 later in the code.
