# AssertionError

**URL:** <https://discourse.julialang.org/t/assertionerror/29690>\
**Category:** General Usage\
**Created:** [October 9, 2019, 7:01pm UTC](https://discourse.julialang.org/t/assertionerror/29690 "2019-10-09T19:01:23Z")\
**Posts on this page:** 11\
**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:** [October 9, 2019, 7:01pm UTC](https://discourse.julialang.org/t/assertionerror/29690/1 "2019-10-09T19:01:23Z")

</div>

i have this piece of code:

```julia
MAT=[1 0;0 2]
n=count(!iszero,MAT)
n0=MAT[1:n]
@assert n0 == ones(n)

```

i ran into an unexpected assertion error for this part, apparently `@assert`didn’t evaluate the assert code line for some reason, and i’m getting for this part:

```julia
AssertionError: n0 == ones(n)

Stacktrace:
 [1] top-level scope at In[15]:4

```

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [October 9, 2019, 7:24pm UTC](https://discourse.julialang.org/t/assertionerror/29690/2 "2019-10-09T19:24:53Z")

</div>

Try

```julia
MAT=[1 0;0 2]
n=count(!iszero,MAT)
n0=MAT[1:n]
@show n0
@show ones(n)
@assert n0 == ones(n)

```

---

<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:** [October 9, 2019, 7:38pm UTC](https://discourse.julialang.org/t/assertionerror/29690/3 "2019-10-09T19:38:49Z")

</div>

your two things aren’t `==`, `@assert` for sure does evaluate before comparison:

```julia
julia> @assert [1,1,1] == ones(2)
ERROR: AssertionError: [1, 1, 1] == ones(2)
Stacktrace:
 [1] top-level scope at REPL[2]:1

julia> @assert [1,1,1] == ones(3)

```

---

<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:** [October 10, 2019, 8:55am UTC](https://discourse.julialang.org/t/assertionerror/29690/4 "2019-10-10T08:55:07Z")

</div>

still got the same assertion error:

```julia
n0 = [1, 0]
ones(n) = [1.0, 1.0]

AssertionError: n0 == ones(n)

Stacktrace:
 [1] top-level scope at In[16]:6

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [October 10, 2019, 9:15am UTC](https://discourse.julialang.org/t/assertionerror/29690/5 "2019-10-10T09:15:48Z")

</div>

Of course it still throws the same assertion error. The `@show` statements are only there to make it easier to see _why_ the assertion fails. (Look at the last element of each vector.)

---

<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:** [October 10, 2019, 9:23am UTC](https://discourse.julialang.org/t/assertionerror/29690/6 "2019-10-10T09:23:06Z")

</div>

yes the last element of the two vectors are differents, then how can i make the vector==`ones(r)`in this case??

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [October 10, 2019, 9:30am UTC](https://discourse.julialang.org/t/assertionerror/29690/7 "2019-10-10T09:30:29Z")

</div>

You will need to rethink how you compute `n`. If this is a homework assignment, then read the text very carefully. Right now you compute `n` to be the number of nonzeros in all of `MAT`. Was this what you were asked to do? Maybe the task was to count only at the _beginning_ of `MAT`?

---

<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:** [October 10, 2019, 9:36am UTC](https://discourse.julialang.org/t/assertionerror/29690/8 "2019-10-10T09:36:40Z")

</div>

it’s not a homework actually it’s a piece of code from a big code where i’m having problems with, and yes `n` is the number of nonzeros in all of `MAT` , after computing `n0` i want to make it as `ones(n)`

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [October 10, 2019, 9:40am UTC](https://discourse.julialang.org/t/assertionerror/29690/9 "2019-10-10T09:40:37Z")

</div>

> [@marouane](#):
>
> i want to make it as `ones(n)`

What do you mean by “it” in this sentence? Do you want to modify the first `n` elements of `MAT`? Here’s how to do that:

```julia
julia> MAT[1:n] .= 1;

julia> MAT
2×2 Array{Int64,2}:
 1 0
 1 2

```

---

<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:** [October 10, 2019, 9:41am UTC](https://discourse.julialang.org/t/assertionerror/29690/10 "2019-10-10T09:41:54Z")

</div>

i mean want to make `n0` equals to `ones(n)`

`n0== ones(n)`

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [October 10, 2019, 9:43am UTC](https://discourse.julialang.org/t/assertionerror/29690/11 "2019-10-10T09:43:45Z")

</div>

Oh. I see. That can be done with:

```julia
n0 = ones(n)

```

after which you can try

```julia
n0 == ones(n) # returns `true`

```
