# Flipping Bits in a Sparse Matrix- Expanding stored entries

**URL:** <https://discourse.julialang.org/t/flipping-bits-in-a-sparse-matrix-expanding-stored-entries/47265>\
**Category:** General Usage\
**Tags:** bit-twiddling, sparse\
**Created:** [September 25, 2020, 2:10pm UTC](https://discourse.julialang.org/t/flipping-bits-in-a-sparse-matrix-expanding-stored-entries/47265 "2020-09-25T14:10:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phillip\_Sutton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phillip_sutton/32/7603_2.png) [@Phillip\_Sutton](https://discourse.julialang.org/u/Phillip_Sutton)\
**Post date:** [September 25, 2020, 2:10pm UTC](https://discourse.julialang.org/t/flipping-bits-in-a-sparse-matrix-expanding-stored-entries/47265/1 "2020-09-25T14:10:02Z")

</div>

My sparse bit array is not dropping non zeros when flipping bits between rows (assignment optimization problem)

```julia
8766×11125 SparseArrays.SparseMatrixCSC{Bool,Int64} with 8766 stored entries:
  [7244, 1] = 1
  [2111, 291] = 1
  [2357, 347] = 1
  [8071, 432] = 1
  ⋮
  [2517, 9187] = 1
  [1756, 9189] = 1
  [1857, 9194] = 1
  [2977, 9195] = 1

```

My flip Bit code is as follows (I’m also open to better ways at doing this!)

```julia
x1=10
x2=20

y1=starts[x1,:].nzind[1]
y2=starts[x2,:].nzind[1]

starts[x1,y1] = false
starts[x2, y2] = false

starts[x1,y2] = true
starts[x2, y1] = true

```

My problem is that starts initially was stoing 8766 Values, and now stores 8768 values

```julia
julia> starts
8766×11125 SparseArrays.SparseMatrixCSC{Bool,Int64} with 8768 stored entries:
  [7244, 1] = 1
  [2111, 291] = 1
  [2357, 347] = 1
  [8071, 432] = 1
  ⋮
  [2517, 9187] = 1
  [1756, 9189] = 1
  [1857, 9194] = 1
  [2977, 9195] = 1

```

notably, its carrying both values

```julia
2×11125 SparseArrays.SparseMatrixCSC{Bool,Int64} with 4 stored entries:
  [1, 7773] = 1
  [2, 7773] = 0
  [1, 9028] = 0
  [2, 9028] = 1

```

How can I continue to only store nonzero (true) values?

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 25, 2020, 2:20pm UTC](https://discourse.julialang.org/t/flipping-bits-in-a-sparse-matrix-expanding-stored-entries/47265/2 "2020-09-25T14:20:56Z")

</div>

`dropzeros!` gets rid of `0` entries. It requires iterating over all the entires though, so if you’re manually flipping bits, there might be a more efficient way to just delete that entry in the array.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 25, 2020, 2:38pm UTC](https://discourse.julialang.org/t/flipping-bits-in-a-sparse-matrix-expanding-stored-entries/47265/3 "2020-09-25T14:38:26Z")

</div>

For this your of operation, csc is a really bad format. I would try using a dok instead as they have much faster insertion and deletion times
