# Zero elements in sparse matrix

**URL:** <https://discourse.julialang.org/t/zero-elements-in-sparse-matrix/97134>\
**Category:** Performance\
**Created:** [April 5, 2023, 8:36pm UTC](https://discourse.julialang.org/t/zero-elements-in-sparse-matrix/97134 "2023-04-05T20:36:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [April 5, 2023, 8:36pm UTC](https://discourse.julialang.org/t/zero-elements-in-sparse-matrix/97134/1 "2023-04-05T20:36:29Z")

</div>

How to find the index of zero elements in a sparse matrix efficiently? I have a matrix with a size of 200k by 200k.

---

<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:** [April 5, 2023, 8:39pm UTC](https://discourse.julialang.org/t/zero-elements-in-sparse-matrix/97134/2 "2023-04-05T20:39:13Z")

</div>

Can you instead find the indices of nonzero elements (since there are way fewer of them)?

---

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [April 5, 2023, 8:54pm UTC](https://discourse.julialang.org/t/zero-elements-in-sparse-matrix/97134/3 "2023-04-05T20:54:00Z")

</div>

That one is easy by just using findnz function.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [April 5, 2023, 9:24pm UTC](https://discourse.julialang.org/t/zero-elements-in-sparse-matrix/97134/4 "2023-04-05T21:24:39Z")

</div>

The issue is that your 200k\*200k matrix has 40 billion entries. You’d need an unusually large RAM to even store the indices of the nonzeros (roughly 320GB for `Int32` coordinates), assuming the nonzeros made up a significant fraction of the matrix (which they must or else the matrix itself would be hundreds of GB).

`findall(iszero, X)` will find them for you, but for the aforementioned reasons will be likely to fail for your large matrix.

You can make an iterator to loop over the nonzero entries

```julia
X = sparse(1:3, 1:3, 1.0)
itr = (I for I in eachindex(X) if iszero(X[I]))

collect(itr) # *for demonstration purposes only - do not collect the iterator*
# 6-element Vector{CartesianIndex{2}}:
# CartesianIndex(2, 1)
# CartesianIndex(3, 1)
# CartesianIndex(1, 2)
# CartesianIndex(3, 2)
# CartesianIndex(1, 3)
# CartesianIndex(2, 3)

```

Again, _do not `collect` the iterator for your large matrix_. Your RAM is unlikely to be big enough. Just use the iterator in a `for` loop or whatever else it was you were planning to do. Or just loop over `eachindex` directly and only do the work when you find a zero.

But more fundamentally, you should see if you can revise what it is you’re trying to do. Doing something with the zeros of a sparse matrix is the opposite of how you should usually try to operate. And looping over 40 billion entries will take a decent amount of time even if you do very little work with each one.
