# How to build this sparsematrix in Julia

**URL:** <https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608>\
**Category:** General Usage\
**Tags:** question, linearalgebra, sparse\
**Created:** [July 21, 2022, 9:20pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608 "2022-07-21T21:20:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 21, 2022, 9:20pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/1 "2022-07-21T21:20:02Z")

</div>

I have the below command in Matlab, I could not find how to do it in Julia. Any suggestion please?

```nohighlight
sparse((1:5)', [11,22,33,44,55], [1,2,3,4,5])

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [July 21, 2022, 9:55pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/2 "2022-07-21T21:55:03Z")

</div>

From `help?> sparse`

```julia
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].

```

Is this what you want?

```julia
julia> sparse(1:5, 1:5, [11,22,33,44,55])
5×5 SparseMatrixCSC{Int64, Int64} with 5 stored entries:
 11 ⋅ ⋅ ⋅ ⋅
  ⋅ 22 ⋅ ⋅ ⋅
  ⋅ ⋅ 33 ⋅ ⋅
  ⋅ ⋅ ⋅ 44 ⋅
  ⋅ ⋅ ⋅ ⋅ 55

```

If so, maybe `Diagonal` would be better choice:

```julia
julia> Diagonal([11,22,33,44,55])
5×5 Diagonal{Int64, Vector{Int64}}:
 11 ⋅ ⋅ ⋅ ⋅
  ⋅ 22 ⋅ ⋅ ⋅
  ⋅ ⋅ 33 ⋅ ⋅
  ⋅ ⋅ ⋅ 44 ⋅
  ⋅ ⋅ ⋅ ⋅ 55

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [July 21, 2022, 10:03pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/4 "2022-07-21T22:03:14Z")

</div>

In that case, it’s even closer to what you do in MATLAB, I’ve just abbreviated your [1,2,3,4,5] to 1:5, but it could be as an array like you have:

```julia
julia> sparse(1:5,[11,22,33,44,55],1:5)
5×55 SparseMatrixCSC{Int64, Int64} with 5 stored entries:
⠀⠀⠀⠀⠀⠁⠀⠀⠀⠀⠐⠀⠀⠀⠀⠀⠄⠀⠀⠀⠀⢀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁

julia> Array(ans)
5×55 Matrix{Int64}:
 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 … 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 21, 2022, 10:08pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/5 "2022-07-21T22:08:39Z")

</div>

@Jeff_Emanuel Thank you very much! Based on this, I have a related issue.

I create matrix `F` and then needs to be updated in a `for loop` as seen below. However, this way is time consuming (due the creation of sparseMatrix at each update). Is there another faster way?

```julia
Nh = [11,22,33,44,55]; # fixed
Ik = [10,12,23,34,45]; # variable
F = sparse(1:5,Nh,Ik)

iter=0;
for _ in 1:100
iter += 1;
Ik = ik*2*iter;
F = F.*iter + sparse(1:5,Nh,Ik)
end

```

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 23, 2022, 12:35am UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/6 "2022-07-23T00:35:50Z")

</div>

I’m getting the following error:

```julia
ERROR: LoadError: UndefVarError: ik not defined

```

Does `ik` change every iteration? Edit: ah maybe a type and it should be `IK`?

---

<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:** [July 23, 2022, 12:58am UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/7 "2022-07-23T00:58:24Z")

</div>

> [@Amro](#):
>
> However, this way is time consuming (due the creation of sparseMatrix at each update). Is there another faster way?

You’re always updating the same entries of your sparse matrix, so it would be vastly more efficient to update the entries in-place by mutating `F.nzval`.

Your code doesn’t make much sense because (even with the `ik` ⟶ `Ik` typo fixed) it yields a zero matrix `F` the end because the integer entries overflow (perhaps you wanted a floating-point matrix instead? unlike Matlab, Julia numbers do not all default to floating-point values), but here is a similar example that updates the entries by adding random numbers:

```julia
irow = 1:5 # row indices
icol = [11,22,33,44,55]; # col indices
F = sparse(irow, icol, rand(5))
for iter = 1:100
    @. F.nzval = iter * F.nzval + rand() # update nonzero entries in-place
end

```

which gives something like:

```julia
julia> @show F;
F = sparse([1, 2, 3, 4, 5], [11, 22, 33, 44, 55], [1.496910562914082e158, 1.8181168960906027e158, 8.993259798670023e157, 1.4533474201806958e158, 1.347203531062703e158], 5, 55)

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 23, 2022, 1:25am UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/8 "2022-07-23T01:25:23Z")

</div>

I am sorry, yes it should be `IK`

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 23, 2022, 2:03am UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/9 "2022-07-23T02:03:30Z")

</div>

It seems like you are only changing the data entries of the sparse matrix during the loop. Hence, you can precompute the data entries, `IK`, and then create the sparse matrix.

Simplifying your math in the code, we can the following simplification of the for loop:

```julia
Ik = [10.0, 12.0, 23.0, 34.0, 45.0] # variable
Ik .*= 2.0^100 * 2 * prod(1:100.0)
F = sparse(1:5, 11:11:55, Ik)

```

Timing gives the following:

```julia
129.232 μs (2447 allocations: 354.84 KiB) # Original code
560.737 ns (13 allocations: 1.80 KiB) # The simplified code above

```

Try to obtain the values of the final sparse matrix and then create a sparse matrix. And when obtaining the values of the final sparse matrix, simplify the mathematical expression.

Note: as your code is posted you will get integer overflow and the resulting matrix will be 0s. Using `prod(1:100.0)` instead of `prod(1:100)`, 2.0^100 instead of 2^100, and Ik = [10.0, …] instead of [10, …] ensures that floats are used and thus the product can be computed successfully, without overflow. (Floats can represent larger numbers in memory compared to Ints)

Since `Nh` is fixed you can also use a UnitRange for it instead of allocating memory for the array. `[11, 22, 33, 44, 55]` turns into `11:11:55`.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 24, 2022, 2:44pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/10 "2022-07-24T14:44:10Z")

</div>

Thank you very much for your replies. I tried to simplify my real problem but it seems I have to mention it exactly as I am afraid I am missing important part. Below is exactly as from my code:

```julia
using SparseArrays, LinearAlgebra
R = sparse([zeros(1,8); Matrix(1.0I, 8-1, 8-1) zeros(8-1,1)]);
Nh = Int8[6, 6, 6, 8, 7, 7]; # fixed
Ik = [0, 1, -4, -0, 4, 1.1];
F = sparse([3 3 3 4 4 4 0.0 0.0;
            1 2 1 7 9 3 0.0 0.0; 
            7 8 1 4 8 8 0.0 0.0;
            1 1 2 8 5 1 0.0 0.0;
            5 6 1 4 2 5 4.6 0.0;
            1 6 8 1 1 4 6.8 0.0]);

for i in 1:100
Ik .+= i; # Here is only a simple example of mutation, 
F .= F * R .+ sparse(1:6, Nh, Ik);
end

```

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 24, 2022, 5:24pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/11 "2022-07-24T17:24:48Z")

</div>

Ah there is a sparse matrix by sparse matrix multiplication happening that was missing in your above examples.

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 24, 2022, 5:26pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/12 "2022-07-24T17:26:15Z")

</div>

Also I am getting the following error:  
`UndefVarError: I not defined`

Can you fix your code above to at least run?

make sure you run the code snippet after posting first. this has happened two times now

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 24, 2022, 5:34pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/14 "2022-07-24T17:34:13Z")

</div>

Do you mind updating your latest code with a minimal example of simple numbers that still uses sparse matrix multiplicatoin. I would prefer to work with a smaller example, instead of with all the numbers, specific to your use case.

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 24, 2022, 6:45pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/16 "2022-07-24T18:45:03Z")

</div>

The code still does not run:  
`ERROR: MethodError: no method matching +(::Vector{Float64}, ::Int64) For element-wise addition, use broadcasting with dot syntax: array .+ scalar `

Can you please check again?

Edit: I think you can replace the offending line with Ik .+= 3

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 24, 2022, 6:50pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/18 "2022-07-24T18:50:54Z")

</div>

> [@Amro](#):
>
> `Ik = i*3; # Here is only a simple example of mutation, `

Shouldn’t Ik be a vector here, instead of a scalar?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 24, 2022, 6:52pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/19 "2022-07-24T18:52:59Z")

</div>

Yes, I am sorry. I have updated it.

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 24, 2022, 6:59pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/20 "2022-07-24T18:59:00Z")

</div>

> [@Amro](#):
>
> `Nh = Int8[6, 6, 6, 8, 7, 7]; # fixed`

You can replace this with just  
`Nh = [6, 6, 6, 8, 7, 7]; # fixed` to reduce the memory use

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 24, 2022, 7:01pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/21 "2022-07-24T19:01:54Z")

</div>

Thank you for your tip. Any way to get ride of creating the sparse matrix at this line?

```julia
F .= F * R .+ sparse(1:6, Nh, Ik);

```

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 24, 2022, 7:17pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/22 "2022-07-24T19:17:46Z")

</div>

> [@Amro](#):
>
> `R = sparse([zeros(1,8); Matrix(1.0I, 8-1, 8-1) zeros(8-1,1)]);`

Instead of creating a dense matrix and then creating a sparse matrix from it, you can directly create a sparse matrix for this line like so:  
`R = sparse(2:8, 1:7, 1., 8, 8)`

I think I have an idea on how to get rid of the for loop even with the matrix multiplication, but it heavily depends on the structure of R

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 24, 2022, 7:23pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/23 "2022-07-24T19:23:14Z")

</div>

Actually, the `for loop` is a time-domain loop in my simulation. So, I think, it is kind of mandatory.

---

<div class="post-metadata">

**Author:** ![acxz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acxz/32/16759_2.png) [@acxz](https://discourse.julialang.org/u/acxz)\
**Post date:** [July 24, 2022, 7:24pm UTC](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608/24 "2022-07-24T19:24:34Z")

</div>

What about the structure of R? Is it an off diagonal diagonal matrix?

[Next page](https://discourse.julialang.org/t/how-to-build-this-sparsematrix-in-julia/84608.md?page=2)
