# Vectorized variable assignment (assign matrix off diagonal values)

**URL:** https://discourse.julialang.org/t/vectorized-variable-assignment-assign-matrix-off-diagonal-values/16363
**Category:** New to Julia
**Tags:** linearalgebra
**Created:** [October 15, 2018, 8:37pm UTC](https://discourse.julialang.org/t/vectorized-variable-assignment-assign-matrix-off-diagonal-values/16363 "2018-10-15T20:37:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Alex\_Ellison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_ellison/32/5749_2.png) [@Alex\_Ellison](https://discourse.julialang.org/u/Alex_Ellison)
#### Post date: [October 15, 2018, 8:37pm UTC](https://discourse.julialang.org/t/vectorized-variable-assignment-assign-matrix-off-diagonal-values/16363/1 "2018-10-15T20:37:53Z")

</div>

Hello world,

I would like to make a matrix with some value A0 down the main diagonal, and some value A1 along the sub/superdiagonal (as defined [Diagonal - Wikipedia](https://en.wikipedia.org/wiki/Diagonal#Matrices)). Is there a way to assign a value to all entries at indices like (i, i+1) or (i, i-1) without an external for loop like:

```julia
for i in 1:n
	out[i, i] = A0
	out[i, 1 + i % n] = A1
	out[1 + i % n, i] = A1
end

```

?

The function setindex! seems to only support assigning value to a square region of your matrix, which isn’t sufficiently general. I also tried playing with LinearIndices, but that seems to be using auxiliary memory proportional to the size of the matrix, which seems silly.

So, what is the best way of assigning some arbitrary set of indices in a matrix one or more values? I suppose there are two cases worth considering: one where I want to set a series of matrix elements to some constant value, and one where we set each element to a corresponding member of another list.

Since Julia boasts such efficiency for numerical/vector ops, I’m hoping there is a syntactically clean way to do this without much memory overhead.

Thanks!  
Alex

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [October 15, 2018, 11:00pm UTC](https://discourse.julialang.org/t/vectorized-variable-assignment-assign-matrix-off-diagonal-values/16363/2 "2018-10-15T23:00:19Z")

</div>

You can use `diagind` from `LinearAlgebra`:

```julia
julia> using LinearAlgebra

julia> out = zeros(5, 5)
5×5 Array{Float64,2}:
 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

julia> out[diagind(out, 0)] .= 1
       out[diagind(out, -1)] .= 2
       out[diagind(out, 1)] .= 3
       out
5×5 Array{Float64,2}:
 1.0 3.0 0.0 0.0 0.0
 2.0 1.0 3.0 0.0 0.0
 0.0 2.0 1.0 3.0 0.0
 0.0 0.0 2.0 1.0 3.0
 0.0 0.0 0.0 2.0 1.0

```

---

<div class="post-metadata">

### Author: ![platawiec](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/platawiec/32/31914_2.png) [@platawiec](https://discourse.julialang.org/u/platawiec)
#### Post date: [October 16, 2018, 2:20pm UTC](https://discourse.julialang.org/t/vectorized-variable-assignment-assign-matrix-off-diagonal-values/16363/3 "2018-10-16T14:20:51Z")

</div>

Since you mention memory overhead, have you had a look at BandedMatrices.jl ?

> **[GitHub - JuliaLinearAlgebra/BandedMatrices.jl: A Julia package for...](https://github.com/JuliaLinearAlgebra/BandedMatrices.jl)**
>
> A Julia package for representing banded matrices. Contribute to JuliaLinearAlgebra/BandedMatrices.jl development by creating an account on GitHub.

---

<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: [October 16, 2018, 3:45pm UTC](https://discourse.julialang.org/t/vectorized-variable-assignment-assign-matrix-off-diagonal-values/16363/4 "2018-10-16T15:45:46Z")

</div>

> [@Alex\_Ellison](#):
>
> I would like to make a matrix with some value A0 down the main diagonal, and some value A1 along the sub/superdiagonal … I’m hoping there is a syntactically clean way to do this without much memory overhead.

You can use the built-in `SymTridiagonal` type, which uses minimal memory and has fast algorithms for things like `A \ b`.

```julia
using LinearAlgebra
A = SymTridiagonal(rmul!(ones(n),A0), rmul!(ones(n-1),A1))

```

Note, however, that there is nothing wrong with using a `for` loop in a pinch. Loops in Julia are fast, just as fast as the “vectorized” operations.

---

<div class="post-metadata">

### Author: ![Alex\_Ellison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_ellison/32/5749_2.png) [@Alex\_Ellison](https://discourse.julialang.org/u/Alex_Ellison)
#### Post date: [October 17, 2018, 2:53am UTC](https://discourse.julialang.org/t/vectorized-variable-assignment-assign-matrix-off-diagonal-values/16363/5 "2018-10-17T02:53:11Z")

</div>

Thank you three for your responses - those thoroughly solve the problem of off diagonal matrix elements!
