# Store a zero in an sparse matrix

**URL:** https://discourse.julialang.org/t/store-a-zero-in-an-sparse-matrix/68515
**Category:** New to Julia
**Tags:** sparse
**Created:** [September 21, 2021, 9:37am UTC](https://discourse.julialang.org/t/store-a-zero-in-an-sparse-matrix/68515 "2021-09-21T09:37:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![carlomontec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlomontec/32/2011_2.png) [@carlomontec](https://discourse.julialang.org/u/carlomontec)
#### Post date: [September 21, 2021, 9:37am UTC](https://discourse.julialang.org/t/store-a-zero-in-an-sparse-matrix/68515/1 "2021-09-21T09:37:55Z")

</div>

Hello,

do you know how can I store a zero in an sparse matrix? it seems that it automatically neglects the zero allocation: `A[1,1]=0.0` is neglected. I know is far from efficient, but the need of it is to debug and replicate an old FORTAN code I am working with.

Thank you.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 21, 2021, 9:44am UTC](https://discourse.julialang.org/t/store-a-zero-in-an-sparse-matrix/68515/2 "2021-09-21T09:44:18Z")

</div>

The point of sparse matrices is not to store zeros, so it’s surprising to me that you’d get a difference based on whether fortran sets an explicit zero - maybe fortran is storing explicitly set zeros? Can you maybe show some more info about what you’re trying to replicate?

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [September 21, 2021, 9:59am UTC](https://discourse.julialang.org/t/store-a-zero-in-an-sparse-matrix/68515/3 "2021-09-21T09:59:47Z")

</div>

It turns out that `getindex(::SparseMatrixCSC, v, i,j)` first checks whether `A[i,j]` is structurally nonzero, and if it isn’t then it turns it into a structural nonzero only if `v` is nonzero, see [https://github.com/JuliaLang/julia/blob/4f3a89e3abecb96bbe221d027278c55dccf23da7/stdlib/SparseArrays/src/sparsematrix.jl#L2634-L2665](https://github.com/JuliaLang/julia/blob/4f3a89e3abecb96bbe221d027278c55dccf23da7/stdlib/SparseArrays/src/sparsematrix.jl#L2634-L2665).

For debugging purposes, you can work around this by first making `A[i,j]` nonzero and then zeroing it out again:

```julia
julia> A = spzeros(3,3);

julia> A[2,2] = 0.0; # This doesn't work

julia> A
3×3 SparseMatrixCSC{Float64, Int64} with 0 stored entries:
  ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ 
  ⋅ ⋅ ⋅ 

julia> A[2,2] = 1.0; # This will make A[2,2] structurally nonzero

julia> A
3×3 SparseMatrixCSC{Float64, Int64} with 1 stored entry:
  ⋅ ⋅ ⋅ 
  ⋅ 1.0 ⋅ 
  ⋅ ⋅ ⋅ 

julia> A[2,2] = 0.0; # Now we get where we wanted

julia> A
3×3 SparseMatrixCSC{Float64, Int64} with 1 stored entry:
  ⋅ ⋅ ⋅ 
  ⋅ 0.0 ⋅ 
  ⋅ ⋅ ⋅ 

```

* * *

To add my unsolicited five cents, I’m not sure whether special-casing `v == 0` in `setindex()` is good design. But it is what it is and is highly unlikely to change anytime soon.

---

<div class="post-metadata">

### Author: ![carlomontec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlomontec/32/2011_2.png) [@carlomontec](https://discourse.julialang.org/u/carlomontec)
#### Post date: [September 21, 2021, 10:00am UTC](https://discourse.julialang.org/t/store-a-zero-in-an-sparse-matrix/68515/4 "2021-09-21T10:00:32Z")

</div>

I am aware that is the whole point of a sparse matrix, but in this case after some coordinate transformations I have as a side product some zeros. My intention is to replicate and compare some sparsity maps as I have in the FORTRAN code.

---

<div class="post-metadata">

### Author: ![carlomontec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlomontec/32/2011_2.png) [@carlomontec](https://discourse.julialang.org/u/carlomontec)
#### Post date: [September 21, 2021, 10:03am UTC](https://discourse.julialang.org/t/store-a-zero-in-an-sparse-matrix/68515/5 "2021-09-21T10:03:22Z")

</div>

Thanks for the idea!

---

<div class="post-metadata">

### Author: ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)
#### Post date: [September 22, 2021, 1:59pm UTC](https://discourse.julialang.org/t/store-a-zero-in-an-sparse-matrix/68515/6 "2021-09-22T13:59:12Z")

</div>

You might use `eps()` as your structural nonzero.
