# Fastest way to fill a Sparse Matrix?

**URL:** <https://discourse.julialang.org/t/fastest-way-to-fill-a-sparse-matrix/79795>\
**Category:** Performance\
**Tags:** question\
**Created:** [April 21, 2022, 5:07pm UTC](https://discourse.julialang.org/t/fastest-way-to-fill-a-sparse-matrix/79795 "2022-04-21T17:07:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alejandro\_Quiaro\_San](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandro_quiaro_san/32/35437_2.png) [@Alejandro\_Quiaro\_San](https://discourse.julialang.org/u/Alejandro_Quiaro_San)\
**Post date:** [April 21, 2022, 5:07pm UTC](https://discourse.julialang.org/t/fastest-way-to-fill-a-sparse-matrix/79795/1 "2022-04-21T17:07:37Z")

</div>

Hello guys, I am wondering which should be the right strategy to fill values in a big sparse matrix.

I have a variable L, which is an n x m matrix, and is sparse. An outer loop runs on the row index, and just a few values of the row are non zero. The non zero values are contained in a vector d, which changes size in every iterations. The pseudo code would be something like this:

```julia
L=zeros(n,m)
for i in 1:m
    for j in 1:n
         L[i,j]=d[j]
    end
end

```

Only 5% of the values are different from zero. If I initialize the matrix L as a Sparse Array (using spzeros), the code becomes slow (I found in another post that the best way to operate with sparse arrays is by just using the Is, Js, ans Vs vectors and build the matrix using the function sparse).

If a try to build a vector, only with the values of d (to then build the matrix L using the function sparse), since the size of d is unknown in each iteration, I have to initialize d as an empty vector that changes size in each loop, making it run slow once again.

For this particular case I can’t find a way to take advantage of the sparse arrays to make the code more efficient.

Any thoughts? Thanks

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 21, 2022, 5:56pm UTC](https://discourse.julialang.org/t/fastest-way-to-fill-a-sparse-matrix/79795/2 "2022-04-21T17:56:25Z")

</div>

> [@Alejandro\_Quiaro\_San](#):
>
> (I found in another post that the best way to operate with sparse arrays is by just using the Is, Js, ans Vs vectors and build the matrix using the function sparse)

This is how I understand it. I tried to translate your problem to use `sparse`

```julia
using BenchmarkTools
using SparseArrays

function test(ds)
    is = Int[]
    js = Int[]
    vs = Float64[]
    for j = 1:size(ds, 2)
        d = ds[:, j]
        dis, dvs = findnz(d)
        for (i, v) in zip(dis, dvs)
            push!(is, i)
            push!(js, j)
            push!(vs, v)
        end
    end
    L = sparse(is, js, vs, size(ds, 1), size(ds, 2))
    @assert L == ds
    L
end

@btime test(ds) setup=(ds=sprand(10000, 10000, 0.05))

```

Does this help?

---

<div class="post-metadata">

**Author:** ![Alejandro\_Quiaro\_San](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandro_quiaro_san/32/35437_2.png) [@Alejandro\_Quiaro\_San](https://discourse.julialang.org/u/Alejandro_Quiaro_San)\
**Post date:** [April 21, 2022, 9:20pm UTC](https://discourse.julialang.org/t/fastest-way-to-fill-a-sparse-matrix/79795/3 "2022-04-21T21:20:38Z")

</div>

Yes, It helped a lot! Thank you so much! the key was in the function:

```julia
push!

```

I was using

```julia
vcat & hcat

```

Which seems to run much more slowly. Thank again for your support.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 21, 2022, 9:29pm UTC](https://discourse.julialang.org/t/fastest-way-to-fill-a-sparse-matrix/79795/4 "2022-04-21T21:29:05Z")

</div>

Yep, `cat` is used to concatenate `Array`s and does more work. But be careful with the order of loops and accesses to your sparse matrix, because it is in (C)ompressed(S)parse(C)olumn format.
