# How to construct a large sparse matrix?

**URL:** <https://discourse.julialang.org/t/how-to-construct-a-large-sparse-matrix/7241>\
**Category:** General Usage\
**Tags:** question, sparse, matrices\
**Created:** [November 22, 2017, 5:11am UTC](https://discourse.julialang.org/t/how-to-construct-a-large-sparse-matrix/7241 "2017-11-22T05:11:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![onetonfoot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/onetonfoot/32/2697_2.png) [@onetonfoot](https://discourse.julialang.org/u/onetonfoot)\
**Post date:** [November 22, 2017, 5:11am UTC](https://discourse.julialang.org/t/how-to-construct-a-large-sparse-matrix/7241/1 "2017-11-22T05:11:17Z")

</div>

Currently, I’m using

```julia
n = 10^5
M = sparse(BitArray(n,n))

```

But when n \> 10^6 I run out of memory so this doesn’t work. I’m unsure what to do as the bellow syntax chucks an error

```julia
SparseMatrixCSC{Bool}(n,n)
MethodError: no method matching SparseMatrixCSC{Bool,Ti} where Ti<:Integer(::Int64, ::Int64)

```

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [November 22, 2017, 6:37am UTC](https://discourse.julialang.org/t/how-to-construct-a-large-sparse-matrix/7241/2 "2017-11-22T06:37:59Z")

</div>

Because `BitArray(n,n)` allocates the 10^6 x 10^6 array and then `sparse` transform it to a (full) sparse matrix

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 22, 2017, 6:39am UTC](https://discourse.julialang.org/t/how-to-construct-a-large-sparse-matrix/7241/3 "2017-11-22T06:39:01Z")

</div>

```julia
help?> sparse                                                                                                                                                            
...
  sparse(I, J, V,[m, n, combine])

  Create a sparse matrix S of dimensions m x n such that S[I[k], J[k]] = V[k]. The combine function is used to combine duplicates. If m and n are not specified, they    
  are set to maximum(I) and maximum(J) respectively. If the combine function is not supplied, combine defaults to + unless the elements of V are Booleans in which case  
  combine defaults to |. All elements of I must satisfy 1 <= I[k] <= m, and all elements of J must satisfy 1 <= J[k] <= n. Numerical zeros in (I, J, V) are retained as  
  structural nonzeros; to drop numerical zeros, use dropzeros!.

  For additional documentation and an expert driver, see Base.SparseArrays.sparse!.                                                                                      
                                                                                                                                                                         
     Example                                                                                                                                                             
    ≡≡≡≡≡≡≡≡≡                                                                                                                                                            

  julia> Is = [1; 2; 3];                                                                                                                                                 
                                                                                                                                                                         
  julia> Js = [1; 2; 3];                                                                                                                                                 
                                                                                                                                                                         
  julia> Vs = [1; 2; 3];                                                                                                                                                 
                                                                                                                                                                         
  julia> sparse(Is, Js, Vs)                                                                                                                                              
  3×3 SparseMatrixCSC{Int64,Int64} with 3 stored entries:                                                                                                                
    [1, 1] = 1                                                                                                                                                         
    [2, 2] = 2                                                                                                                                                         
    [3, 3] = 3                                                                                                                                                         

```

Also, `sprand` might be of use.

---

<div class="post-metadata">

**Author:** ![onetonfoot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/onetonfoot/32/2697_2.png) [@onetonfoot](https://discourse.julialang.org/u/onetonfoot)\
**Post date:** [November 22, 2017, 10:18am UTC](https://discourse.julialang.org/t/how-to-construct-a-large-sparse-matrix/7241/4 "2017-11-22T10:18:56Z")

</div>

Thanks, I was looking for

```julia
n = 10^6
sparse([],[],Bool[],n,n)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 22, 2017, 10:49am UTC](https://discourse.julialang.org/t/how-to-construct-a-large-sparse-matrix/7241/5 "2017-11-22T10:49:17Z")

</div>

Note that inserting elements on a non stored zero in a sparse matrix is quite expensive. If possible it might be worth building the matrix from `I, J, V` and then convert it to a sparse matrix to do operations that does not change the sparsity pattern.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 22, 2017, 1:28pm UTC](https://discourse.julialang.org/t/how-to-construct-a-large-sparse-matrix/7241/6 "2017-11-22T13:28:08Z")

</div>

> [@kristoffer.carlsson](#):
>
> If possible it might be worth building the matrix from I, J, V and then convert it to a sparse matrix to do operations that does not change the sparsity pattern.

In some applications you can use other construction methods too, e.g. `spdiagm` for sparse matrices with a banded structure, or Kronecker products (`kron`) of smaller sparse matrices for sparse matrices arising from regular grids (such as [in finite-difference schemes](http://math.mit.edu/~stevenj/18.303/lecture-10.pdf)).

---

<div class="post-metadata">

**Author:** ![phymann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phymann/32/18868_2.png) [@phymann](https://discourse.julialang.org/u/phymann)\
**Post date:** [November 4, 2021, 4:58pm UTC](https://discourse.julialang.org/t/how-to-construct-a-large-sparse-matrix/7241/7 "2021-11-04T16:58:20Z")

</div>

thank you for these extra tips, really helpful!
