# Efficiently updating values in a sparse diagonal matrix

**URL:** <https://discourse.julialang.org/t/efficiently-updating-values-in-a-sparse-diagonal-matrix/13111>\
**Category:** General Usage\
**Created:** [August 9, 2018, 6:52pm UTC](https://discourse.julialang.org/t/efficiently-updating-values-in-a-sparse-diagonal-matrix/13111 "2018-08-09T18:52:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Norman](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@Norman](https://discourse.julialang.org/u/Norman)\
**Post date:** [August 9, 2018, 6:52pm UTC](https://discourse.julialang.org/t/efficiently-updating-values-in-a-sparse-diagonal-matrix/13111/1 "2018-08-09T18:52:04Z")

</div>

Let’s say I have 5 vectors A, B, C, D, E and I build a sparse matrix as follows:

```
M = spdiagm(0 => A, -1 => B, 1 => C, -10 => D, 10 => E)

```

So, as you see, all the nonzero entries are on the five diagonals. (So, the number of nonzero entries is fixed.) In my actual usage, the entries in vectors A, B, C are updated repetitively in a loop. Every time the vectors are updated, I need the M matrix to be updated as well. Currently, I just use spdiagm() to generate a new matrix repetitively in the loop. Since the matrix could be quite large sometimes, I thought it would be much more efficient if there is a way to update the nonzero entries of M in-place, without creating a new matrix every time. Is there such a way to update the entries of the sparse matrix without creating a new one? Thanks!

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [August 9, 2018, 7:29pm UTC](https://discourse.julialang.org/t/efficiently-updating-values-in-a-sparse-diagonal-matrix/13111/2 "2018-08-09T19:29:48Z")

</div>

You can directly manipulate the `nzval` field of the sparse matrix. The manual chapter on `SparseArrays` describes the storage format.

```julia
julia> w
5×5 SparseMatrixCSC{Float64,Int64} with 5 stored entries:
  [1, 1] = 1.0
  [2, 2] = 2.0
  [3, 3] = 3.0
  [4, 4] = 4.0
  [5, 5] = 9.0

julia> w.nzval[1] = 7
7

julia> w
5×5 SparseMatrixCSC{Float64,Int64} with 5 stored entries:
  [1, 1] = 7.0
  [2, 2] = 2.0
  [3, 3] = 3.0
  [4, 4] = 4.0
  [5, 5] = 9.0

```

---

<div class="post-metadata">

**Author:** ![Norman](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@Norman](https://discourse.julialang.org/u/Norman)\
**Post date:** [August 9, 2018, 7:50pm UTC](https://discourse.julialang.org/t/efficiently-updating-values-in-a-sparse-diagonal-matrix/13111/3 "2018-08-09T19:50:41Z")

</div>

Thanks for your reply.

The issue with directly handling nzval is that the entries are ordered by columns rather than by diagonal. I think I will either need to rewrite the way the diagonals A, B, C, D, E are created to accommodate the column order, or find a function to do the reordering.
