# Saving sparse matrix

**URL:** <https://discourse.julialang.org/t/saving-sparse-matrix/28089>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 28, 2019, 8:00am UTC](https://discourse.julialang.org/t/saving-sparse-matrix/28089 "2019-08-28T08:00:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![saeed](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@saeed](https://discourse.julialang.org/u/saeed)\
**Post date:** [August 28, 2019, 8:00am UTC](https://discourse.julialang.org/t/saving-sparse-matrix/28089/1 "2019-08-28T08:00:01Z")

</div>

How to save a sparse matrix in Julia? I want the saved file to be small since my Matrix size is about (2^26,2^26).  
Thanks.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 28, 2019, 8:19am UTC](https://discourse.julialang.org/t/saving-sparse-matrix/28089/2 "2019-08-28T08:19:55Z")

</div>

I would just save nonzero indexes and values as CSV:

```julia
using SparseArrays, DataFrames, CSV
M = sprand(10, 10, 0.2)
I, J, V = findnz(M)
df = DataFrame([:I => I, :J => J, :V => V])
CSV.write("/tmp/spmatrix.csv", df)

```

---

<div class="post-metadata">

**Author:** ![saeed](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@saeed](https://discourse.julialang.org/u/saeed)\
**Post date:** [August 29, 2019, 3:56am UTC](https://discourse.julialang.org/t/saving-sparse-matrix/28089/3 "2019-08-29T03:56:47Z")

</div>

Thank you, the problem is that CSV file is very large, for example in python I use \*.npz which is 2-5 MB but here it can go up to 500 MB.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 29, 2019, 5:37am UTC](https://discourse.julialang.org/t/saving-sparse-matrix/28089/4 "2019-08-29T05:37:42Z")

</div>

I don’t know this format, but you can also compress the CSV, eg with gzip.

Alternatively, you can save a bit of space by using the CSC representation (ie export directly from the fields), but I don’t think it is worth it.

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [May 25, 2021, 4:51am UTC](https://discourse.julialang.org/t/saving-sparse-matrix/28089/5 "2021-05-25T04:51:42Z")

</div>

> [@Tamas\_Papp](#):
>
> I would just save nonzero indexes and values as CSV:

Great answer. Minor remark in passing: with `LinearAlgebra.I`, `I` is no longer a great choice in `I, J, V`. 😥 `R, C, V` or `M, N, V` are viable alternatives I’ve seen used.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 25, 2021, 6:09am UTC](https://discourse.julialang.org/t/saving-sparse-matrix/28089/6 "2021-05-25T06:09:29Z")

</div>

For production code I would wrap it in a function so this should not matter.

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [May 25, 2021, 9:27am UTC](https://discourse.julialang.org/t/saving-sparse-matrix/28089/7 "2021-05-25T09:27:32Z")

</div>

> [@Tamas\_Papp](#):
>
> I would wrap it in a function so this should not matter.

Very true!

```
using SparseArrays, DataFrames, CSV, LinearAlgebra
function save_sparse_matrix(M)
    I, J, V = findnz(M)
    println("I = ", I)
    df = DataFrame([:I => I, :J => J, :V => V])
    CSV.write("M.csv", df)
end

julia> M = sprand(10, 10, 0.2)

julia> save_sparse_matrix(M)
I = [1, 7, 5, 9, 10, 1, 3, 4, 6, 3, 4, 9, 3, 7, 10, 5, 6, 7, 9, 10, 7]

julia> I
UniformScaling{Bool}
true*I

```

Thanks. 😃
