# Creating a sparse matrix from find operation on dense matrix

**URL:** <https://discourse.julialang.org/t/creating-a-sparse-matrix-from-find-operation-on-dense-matrix/61056>\
**Category:** General Usage\
**Tags:** question, sparse\
**Created:** [May 13, 2021, 1:19am UTC](https://discourse.julialang.org/t/creating-a-sparse-matrix-from-find-operation-on-dense-matrix/61056 "2021-05-13T01:19:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [May 13, 2021, 1:19am UTC](https://discourse.julialang.org/t/creating-a-sparse-matrix-from-find-operation-on-dense-matrix/61056/1 "2021-05-13T01:19:28Z")

</div>

I have a large matrix `x`. I’d like to create a sparse representation of the values that meet a certain criteria, e.g. `xi < 1`.

I’d like to do something like the following:

```julia
x = rand(1000, 1000)
i = findall(xi -> xi < 0.01, x)
xs = sparse(x, i)

```

I wasn’t able to find a sparse constructor that used `CartesianIndices`. What is the recommended way to do this? Ideally without copying values from x (though I’m not sure this would be faster).

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [May 13, 2021, 1:42am UTC](https://discourse.julialang.org/t/creating-a-sparse-matrix-from-find-operation-on-dense-matrix/61056/2 "2021-05-13T01:42:16Z")

</div>

This should work:

```julia
sparse(x .* (x .< 0.01))

```

Otherwise, if you want to go through the indices, then you could so something like

```julia
CI = findall(<(0.01), x)
using Unzip # not sure it's needed but I find it convenient
i, j = unzip(Tuple.(CI))
xs = sparse(i, j, x[CI], size(x)...)

```

---

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [May 13, 2021, 1:55am UTC](https://discourse.julialang.org/t/creating-a-sparse-matrix-from-find-operation-on-dense-matrix/61056/3 "2021-05-13T01:55:39Z")

</div>

Thanks for the lightning response and the slick one-liner!

Here’s a quick comparison of the hack I have been working with, your two approaches, and one i found [here](https://discourse.julialang.org/t/cartesian-indices-linear-indices-and-sparse-matrices/13781).

```julia
using BenchmarkTools, SparseArrays, Unzip
x = rand(1000,1000)
d = 0.01

@btime begin
    m = x .< d
    i,j, = findnz(m)
    xs = sparse(i ,j, view(x, m), size(x)...)
end
# 1.489 ms (38 allocations: 851.00 KiB)

@btime xs = sparse(x .* (x .< d))
# 2.482 ms (12 allocations: 7.79 MiB)

@btime begin
    CI = findall(x .< d)
    i, j = unzip(Tuple.(CI))
    xs = sparse(i, j, view(x, CI), size(x)...)
end
# 1.190 ms (45 allocations: 927.47 KiB)

@btime begin
    CI = findall(x .< d)
    CI′ = reinterpret(Int, reshape(CI, 1, :))
    xs = sparse(view(CI′, 1, :), view(CI′, 2, :), view(x, CI), size(x)...)
end
# 1.044 ms (40 allocations: 617.20 KiB)

```

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [May 13, 2021, 2:07am UTC](https://discourse.julialang.org/t/creating-a-sparse-matrix-from-find-operation-on-dense-matrix/61056/4 "2021-05-13T02:07:10Z")

</div>

I did not make any promises on performance! 😅

FWIW, I reckon doing `findall(x .< d)` is actually better than `findall(<(d), x)`. Maybe try to compare with this?

```julia
CI = findall(x .< d)
i, j = unzip(Tuple.(CI))
sparse(i, j, view(x,CI), size(x)...)

```

But I should let the performance pros chime in!

---

<div class="post-metadata">

**Author:** ![bmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmit/32/12443_2.png) [@bmit](https://discourse.julialang.org/u/bmit)\
**Post date:** [May 13, 2021, 2:20am UTC](https://discourse.julialang.org/t/creating-a-sparse-matrix-from-find-operation-on-dense-matrix/61056/5 "2021-05-13T02:20:04Z")

</div>

You’re right. Updated the above per your suggestions
