# Constructing a block tri-diagonal matrix

**URL:** https://discourse.julialang.org/t/constructing-a-block-tri-diagonal-matrix/36704
**Category:** General Usage
**Tags:** question, linearalgebra
**Created:** [March 29, 2020, 11:00pm UTC](https://discourse.julialang.org/t/constructing-a-block-tri-diagonal-matrix/36704 "2020-03-29T23:00:41Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [March 29, 2020, 11:00pm UTC](https://discourse.julialang.org/t/constructing-a-block-tri-diagonal-matrix/36704/1 "2020-03-29T23:00:41Z")

</div>

I need to construct a matrix of this form:

![image](https://global.discourse-cdn.com/julialang/original/3X/9/b/9b6eace313a70180918811401e741fe573307bf8.png)

where `A1, A2, ..., A_M` are given (rectangular) matrices, and the zeros are blocks of appropriate sizes.

How can I do this in Julia, with as less code as possible? Maybe there is a package I can use?

There is the function `SparseArrays.blockdiag` which does something close, but only works for sparse arrays. The matrices I have are not sparse.

A generalization of this is to construct block tridiagonal matrices ([Block matrix - Wikipedia](https://en.wikipedia.org/wiki/Block_matrix#Block_tridiagonal_matrices)). But I haven’t found any packages for this.

---

<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: [March 29, 2020, 11:12pm UTC](https://discourse.julialang.org/t/constructing-a-block-tri-diagonal-matrix/36704/2 "2020-03-29T23:12:03Z")

</div>

> [@e3c6](#):
>
> There is the function `SparseArrays.blockdiag` which does just this, but only works for sparse arrays. The matrices I have are not sparse.

Well, you can always call `Matrix` on the result to convert the final result to a dense matrix. (This is not the most efficient possible way, but you asked for a minimal-code solution.)

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [March 29, 2020, 11:24pm UTC](https://discourse.julialang.org/t/constructing-a-block-tri-diagonal-matrix/36704/3 "2020-03-29T23:24:50Z")

</div>

Actually `blockdiag` doesn’t let me interleave the `0` blocks, which are shared in my case between one block and the next. Also for large matrices the dense → sparse → dense conversions are too slow, I think.

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [March 29, 2020, 11:28pm UTC](https://discourse.julialang.org/t/constructing-a-block-tri-diagonal-matrix/36704/4 "2020-03-29T23:28:22Z")

</div>

Have you tried [BlockBandedMatrices.jl](https://github.com/JuliaMatrices/BlockBandedMatrices.jl)?

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [March 29, 2020, 11:31pm UTC](https://discourse.julialang.org/t/constructing-a-block-tri-diagonal-matrix/36704/5 "2020-03-29T23:31:39Z")

</div>

Maybe I did not understand this package well, I just read the docs now. But I think it deals with matrices where the blocks are banded? I rather need a to build a matrix the bands are blocks.

---

<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: [March 29, 2020, 11:33pm UTC](https://discourse.julialang.org/t/constructing-a-block-tri-diagonal-matrix/36704/6 "2020-03-29T23:33:55Z")

</div>

[https://juliaarrays.github.io/BlockArrays.jl/stable/man/blockarrays/](https://juliaarrays.github.io/BlockArrays.jl/stable/man/blockarrays/)

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [March 29, 2020, 11:38pm UTC](https://discourse.julialang.org/t/constructing-a-block-tri-diagonal-matrix/36704/7 "2020-03-29T23:38:29Z")

</div>

Can you show an example? I’m not sure how I can use this package to do what I want.

---

<div class="post-metadata">

### Author: ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)
#### Post date: [March 31, 2020, 8:16pm UTC](https://discourse.julialang.org/t/constructing-a-block-tri-diagonal-matrix/36704/8 "2020-03-31T20:16:01Z")

</div>

```julia
julia> using BlockBandedMatrices
[Info: Precompiling BlockBandedMatrices [ffab5731-97b5-5995-9138-79e8c1846df0]

julia> A = BlockTridiagonal(fill([1 2],3), fill([3 4],4), fill([4 5],3))
4×4-blocked 4×8 BlockArrays.BlockArray{Int64,2,Tridiagonal{Array{Int64,2},Array{Array{Int64,2},1}},Tuple{BlockArrays.BlockedUnitRange{Array{Int64,1}},BlockArrays.BlockedUnitRange{Array{Int64,1}}}}:
 3 4 │ 4 5 │ ⋅ ⋅ │ ⋅ ⋅
 ──────┼────────┼────────┼──────
 1 2 │ 3 4 │ 4 5 │ ⋅ ⋅
 ──────┼────────┼────────┼──────
 ⋅ ⋅ │ 1 2 │ 3 4 │ 4 5
 ──────┼────────┼────────┼──────
 ⋅ ⋅ │ ⋅ ⋅ │ 1 2 │ 3 4

```
