# Nnz function appears not to be working

**URL:** <https://discourse.julialang.org/t/nnz-function-appears-not-to-be-working/15793>\
**Category:** New to Julia\
**Created:** [October 2, 2018, 3:05pm UTC](https://discourse.julialang.org/t/nnz-function-appears-not-to-be-working/15793 "2018-10-02T15:05:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![threadedblue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/threadedblue/32/19247_2.png) [@threadedblue](https://discourse.julialang.org/u/threadedblue)\
**Post date:** [October 2, 2018, 3:05pm UTC](https://discourse.julialang.org/t/nnz-function-appears-not-to-be-working/15793/1 "2018-10-02T15:05:44Z")

</div>

Can anyone explain this?

I have an array (See below) that when it gets passed to the nnz() function; the return value is 15, which is not the number of non zeros but the the array total. Should it not return 7? I am using v1.0.1.

Thanks

My array as displayed by Jupyter:  
3×5 SparseArrays.SparseMatrixCSC{Any,Int64} with 15 stored entries:  
[1, 1] = 4  
[2, 1] = 0  
[3, 1] = 3  
[1, 2] = 2  
[2, 2] = 0  
[3, 2] = 2  
[1, 3] = 4  
[2, 3] = 0  
[3, 3] = 1  
[1, 4] = 0  
[2, 4] = 0  
[3, 4] = 0  
[1, 5] = 0  
[2, 5] = 0  
[3, 5] = 1

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 2, 2018, 3:24pm UTC](https://discourse.julialang.org/t/nnz-function-appears-not-to-be-working/15793/2 "2018-10-02T15:24:07Z")

</div>

> [@threadedblue](#):
>
> SparseArrays.SparseMatrixCSC{Any,Int64}

I think you want:

```julia
A = sparse([1, 2, 3], [1, 2, 3], [0, 2, 0])
nnz(A) # returns 3 (stored elements)
count(!iszero, A) # returns 1 (two of the stored elements are zero

```

Have a look at the documentation [here](https://docs.julialang.org/en/v1/stdlib/SparseArrays/index.html).

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [October 2, 2018, 3:36pm UTC](https://discourse.julialang.org/t/nnz-function-appears-not-to-be-working/15793/3 "2018-10-02T15:36:31Z")

</div>

The documentation for `SparseArrays.nnz` says:

> Returns the number of stored (filled) elements in a sparse array.

So the zeros that are explicitly stored in the `SparseMatrixCSC` are counted as well.

---

<div class="post-metadata">

**Author:** ![threadedblue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/threadedblue/32/19247_2.png) [@threadedblue](https://discourse.julialang.org/u/threadedblue)\
**Post date:** [October 2, 2018, 3:39pm UTC](https://discourse.julialang.org/t/nnz-function-appears-not-to-be-working/15793/4 "2018-10-02T15:39:32Z")

</div>

Thanks for responding. However, because I am working with a third party library, I am stuck with the array as you see above. I did try a conversion.  
A = sparse(Matrix(myarray))  
nnz(A)  
7

Correct answer to be sure, but I’d like to better understand why?  
typeof(myarray) yields SparseArrays.SparseMatrixCSC  
typeof(A) also yields SparseArrays.SparseMatrixCSC

Why the difference?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 2, 2018, 3:42pm UTC](https://discourse.julialang.org/t/nnz-function-appears-not-to-be-working/15793/5 "2018-10-02T15:42:17Z")

</div>

You can also use `dropzeros!`

```julia
help?> dropzeros!
search: dropzeros! dropzeros

  dropzeros!(A::SparseMatrixCSC; trim::Bool = true)

  Removes stored numerical zeros from A, optionally trimming resulting excess space from A.rowval and A.nzval when trim is true.

  For an out-of-place version, see dropzeros. For algorithmic information, see fkeep!.

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 2, 2018, 5:12pm UTC](https://discourse.julialang.org/t/nnz-function-appears-not-to-be-working/15793/6 "2018-10-02T17:12:59Z")

</div>

Apologies, maybe my response wasn’t clear - the `sparse()` function simply produces an Array of type `SparseArrays.SparseMatrixCSC`, equivalent to what you have (as you noticed from calling `typeof`). I only used it to create a minimum working example.

What I (and the other posters in this thread) have tried to point out is that the `nnz` function does not do what you think it should do - it returns the number of elements in your sparse matrix which are “filled”, independent of whether you filled them with a zero.

I simply tried to point out that the behaviour you were apparently looking for (if I understand you correctly) is provided by a different function, `count(!iszero, A)` - this returns the number of all non-zero elements in your matrix. As Kristoffer says above, you can achieve a similar result by calling `dropzero` on your matrix:

```julia
julia> using SparseArrays

julia> A = sparse([1,2,3],[1,2,3],[0,2,0])
3×3 SparseMatrixCSC{Int64,Int64} with 3 stored entries:
  [1, 1] = 0
  [2, 2] = 2
  [3, 3] = 0

julia> nnz(A)
3

julia> count(!iszero, A)
1

julia> B = dropzeros(A)
3×3 SparseMatrixCSC{Int64,Int64} with 1 stored entry:
  [2, 2] = 2

julia> nnz(B)
1

```
