# Flipping Bits in a Sparse Matrix- Expanding stored entries

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

``````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!)

``````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> 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

``````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?

`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.

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