# Initializing a matrix of matrices

**URL:** https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743
**Category:** New to Julia
**Created:** [January 10, 2023, 2:13am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743 "2023-01-10T02:13:48Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![dfb1002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfb1002/32/43827_2.png) [@dfb1002](https://discourse.julialang.org/u/dfb1002)
#### Post date: [January 10, 2023, 2:13am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/1 "2023-01-10T02:13:48Z")

</div>

Hi everyone,

I have found these posts:

> [@Correctly initializing collection of matrices](https://discourse.julialang.org/t/correctly-initializing-collection-of-matrices/62116):
>
> Hello! I am following suggestions from another post and, instead of having a 3-D array, I am refactoring my code to use a Vector of Matrix so I can use broadcasting on the matrices. Is there a way to initialize the whole structure so I can fill its values later? I could do this for 3D case: S = Array{ComplexF64,3}(undef,4,4,1000) But I can’t find a way to do it for the new code. Is it possible? Is there lack of performance if I don’t initialize it? The advantage is that I guess I could alwa…

> [@Pluggin a series of small matrices into a larger matrix (with addition)](https://discourse.julialang.org/t/pluggin-a-series-of-small-matrices-into-a-larger-matrix-with-addition/74255):
>
> What is the best way to add a series of smaller matrices to a particular point of a bigger matrix? For example: Starting with an empty matrix of zeros: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I would like to insert the following matrices into the larger matrix, and add elements where the matrices overlap Small 1: 1 1 1 1 Small 2: 2 2 2 2 Small 3: 3 3 3 3 Such that the final result looks like this: 1 1 0 0 1 3 2 0 0 2 5 3 0 0 3 3 Any help would be appreciated.

Neither seems to do exactly what I want. I am trying to create a matrix of matrices. I need the structure to be in this way for what I am working on. I will provide an example. What I am trying to do is the following:

```M

for j = 1:2
      M[1,j] = ones(2,2)
end

for j = 1:2
     M[2,j] = [2 2 ; 2 2]
end

```

I am doing a larger example, but the error message is the same. The error I am getting is MethodError: Cannot ‘convert’ object of type Matrix{Int64} to an object of type Float64.

I know exactly why the error is occurring. When I initialize M with the zeros command, it creates a matrix of floats. Therefore, when I try to replace an entry, it expects floats, but I am trying to give it matrices. To try on a smaller example, I then tried initializing M as follows:

```julia
M = [[0 0 ; 0 0] [0 0 ; 0 0] ; [0 0 ; 0 0] [0 0 ; 0 0]

```

The above is what I want, a matrix within a matrix. However, when I find M[1,1], for example, it gives me the single entry 0. I want

```julia
M[1,1] = [0 0 ; 0 0]

```

as it is the first component of the big matrix M. Is there a way I can do this?  
Thanks!

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [January 10, 2023, 2:21am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/2 "2023-01-10T02:21:01Z")

</div>

Why do you want a matrix of matrices, when you can simply have one big matrix?

```julia
julia> M = [[0 0 ; 0 0] [0 0 ; 0 0] ; [0 0 ; 0 0] [0 0 ; 0 0] ]
4×4 Matrix{Int64}:
 0 0 0 0
 0 0 0 0
 0 0 0 0
 0 0 0 0

julia> M[1:2, 1:2]
2×2 Matrix{Int64}:
 0 0
 0 0

```

If you insist, though…

```julia
julia> M = [[0 0; 0 0] for i=1:2, j=1:2]
2×2 Matrix{Matrix{Int64}}:
 [0 0; 0 0] [0 0; 0 0]
 [0 0; 0 0] [0 0; 0 0]

julia> M[1, 1]
2×2 Matrix{Int64}:
 0 0
 0 0

```

---

<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: [January 10, 2023, 2:28am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/3 "2023-01-10T02:28:56Z")

</div>

```julia
julia> M = [zeros(2,2) for i = 1:2, j=1:2]
2×2 Matrix{Matrix{Float64}}:
 [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> M[1,1]
2×2 Matrix{Float64}:
 0.0 0.0
 0.0 0.0

```

Note that if you are working with lots of tiny matrices like this you may want to consider [StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl), which can be much more efficient for small matrices and vectors of fixed sizes:

```julia
julia> using StaticArrays

julia> M = [@SMatrix(zeros(2,2)) for i = 1:2, j=1:2]
2×2 Matrix{SMatrix{2, 2, Float64, 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]

julia> M = fill(@SMatrix(zeros(2,2)), 2,2)
2×2 Matrix{SMatrix{2, 2, Float64, 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]

julia> M = zeros(SMatrix{2,2,Float64,4}, 2,2)
2×2 Matrix{SMatrix{2, 2, Float64, 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]

```

---

<div class="post-metadata">

### Author: ![dfb1002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfb1002/32/43827_2.png) [@dfb1002](https://discourse.julialang.org/u/dfb1002)
#### Post date: [January 10, 2023, 2:51am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/4 "2023-01-10T02:51:55Z")

</div>

I would need to provide context to the problem, but I do have a reason for preferring the matrix of matrices. However, it seems I’ve run into an issue I initially didn’t expect. I need to take the inverse of the matrix I’m working with (it’s not all zeros in my case), and the inverse exists globally, but not over each block. Therefore, what I need to do is convert the matrix into the full 4 x 4, take the inverse, and then go back to a matrix of matrices to do the operation I need. Is there a way to do that? Sorry, I didn’t expect this issue initially. Since it’s a different question, I can accept this one and make a new thread if that’s better.

---

<div class="post-metadata">

### Author: ![dfb1002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfb1002/32/43827_2.png) [@dfb1002](https://discourse.julialang.org/u/dfb1002)
#### Post date: [January 10, 2023, 2:53am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/5 "2023-01-10T02:53:33Z")

</div>

I will keep this in mind! At the moment I don’t have a pressing need to do this all the time, but these types of problems I will be solving a lot in the future. I may need this!

Does the package have a way to convert the matrix of matrices into a full matrix (so a 4 x 4 in this case) and then back to the 2 x 2? I found out I need to take the inverse, and the inverse exist globally, but not in each block. Therefore, I need to get the full matrix, and then convert back.

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [January 10, 2023, 3:07am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/6 "2023-01-10T03:07:01Z")

</div>

Can you just leave everything in a single large matrix, and operate on the submatrices through views?

```julia
struct View{A} a::A end # indexable `View` object
Base.size(v::View) = size(v.a)
Base.getindex(v::View, args...) = view(v.a, args...)

M = [i+j for i=1:4, j=0:4:12]

# construct sub-matrices 
vm = View(M)
A, B, C, D = vm[1:2, 1:2], vm[1:2, 3:4], vm[3:4, 1:2], vm[3:4, 3:4]

# do stuff with sub-matrices
A .+= C
D .*= B
M

```

---

<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: [January 10, 2023, 3:09am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/8 "2023-01-10T03:09:18Z")

</div>

> [@dfb1002](#):
>
> Does the package have a way to convert the matrix of matrices into a full matrix (so a 4 x 4 in this case) and then back to the 2 x 2? I found out I need to take the inverse, and the inverse exist globally, but not in each block. Therefore, I need to get the full matrix, and then convert back.

Sure, this is possible. But this is sounding more and more like an [XY problem](https://xyproblem.info/) … what underlying problem are you actually trying to solve?

---

<div class="post-metadata">

### Author: ![dfb1002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfb1002/32/43827_2.png) [@dfb1002](https://discourse.julialang.org/u/dfb1002)
#### Post date: [January 10, 2023, 3:09am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/9 "2023-01-10T03:09:52Z")

</div>

In this small example I could, but in general I’m doing this with large matrices. I see you’re defining A,B,C, and D all separately. I don’t want to have to do that 25+ times! Converting would be easiest, but I don’t know if that’s possible

---

<div class="post-metadata">

### Author: ![dfb1002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfb1002/32/43827_2.png) [@dfb1002](https://discourse.julialang.org/u/dfb1002)
#### Post date: [January 10, 2023, 3:11am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/10 "2023-01-10T03:11:03Z")

</div>

I mean I could take multiple paragraphs to explain if you’d like, but I’d rather avoid that. Could you explain the way I’d do it? I think this is the last step in my problem!

---

<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: [January 10, 2023, 3:11am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/11 "2023-01-10T03:11:42Z")

</div>

> [@dfb1002](#):
>
> I see you’re defining A,B,C, and D all separately. I don’t want to have to do that 25+ times!

Julia has lots of looping constructs, so you would not have to do this one at a time. But again, can you give us a bit of background on what problem you are trying to solve?

---

<div class="post-metadata">

### Author: ![dfb1002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfb1002/32/43827_2.png) [@dfb1002](https://discourse.julialang.org/u/dfb1002)
#### Post date: [January 10, 2023, 3:14am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/13 "2023-01-10T03:14:11Z")

</div>

It would take too long. This is nearly the ending step of a multiple step problem. It would take me probably 3-4 paragraphs to explain and show everything. I don’t want to take that much of your time. I believe this is the last step, there’s no more additional functions after 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: [January 10, 2023, 3:15am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/14 "2023-01-10T03:15:29Z")

</div>

> [@dfb1002](#):
>
> I mean I could take multiple paragraphs to explain if you’d like, but I’d rather avoid that. Could you explain the way I’d do it? I think this is the last step in my problem!

Like

```julia
julia> n = 100
100

julia> M = rand(n,n); # 100x100 matrix

julia> M2 = [@view(M[2i-1:2i,2j-1:2j]) for i=1:n÷2, j=1:n÷2]; # 50x50 matrix of 2x2 blocks

julia> M2[1,1]
2×2 view(::Matrix{Float64}, 1:2, 1:2) with eltype Float64:
 0.663167 0.712037
 0.0853913 0.887108

julia> M[1:2,1:2]
2×2 Matrix{Float64}:
 0.663167 0.712037
 0.0853913 0.887108

```

---

<div class="post-metadata">

### Author: ![dfb1002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfb1002/32/43827_2.png) [@dfb1002](https://discourse.julialang.org/u/dfb1002)
#### Post date: [January 10, 2023, 3:21am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/16 "2023-01-10T03:21:12Z")

</div>

Nearly there! Now I see how to get from the full matrix to the small 2 x 2 ones. The last thing I need is how to get from the small 2 x 2 ones to the full matrix. I need to do that first since I’m storing the 2 x 2 form with my loop. Unless you believe there’s an easier way to refine my earlier loop in my first post to begin with the full matrix? I’m open to that as well! Then I’ll be all set

---

<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: [January 10, 2023, 3:39am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/17 "2023-01-10T03:39:30Z")

</div>

Well, if `B` is a matrix of matrices, e.g. `B = [rand(2,2) for i = 1:50, j=1:50]`, then you could convert it to a single 100x100 matrix `M` with e.g.:

```julia
M = zeros(100,100)
for i=1:50, j=1:50
    M[2i-1:2i,2j-1:2j] = B[i,j]
end

```

Another option is to use the [BlockArrays.jl package](https://github.com/JuliaArrays/BlockArrays.jl). Then if you have an array of arrays, like `B` above, you can treat it as a single 100x100 matrix (and do linear algebra directly) with `mortar(B)`, or convert it to an ordinary matrix with `Matrix(mortar(B))`.

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [January 10, 2023, 6:01am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/18 "2023-01-10T06:01:59Z")

</div>

I would use `@views` here.

```julia
@views A, B, C, D = M[1:2, 1:2], M[1:2, 3:4], M[3:4, 1:2], M[3:4, 3:4]

```

produces the same result.

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [January 10, 2023, 6:13am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/19 "2023-01-10T06:13:00Z")

</div>

It can also be done this way:

```julia
julia> M = [i+j for i=1:4, j=0:4:12];

julia> B = [M[i:i+1, j:j+1] for i in 1:2:size(M,1), j in 1:2:size(M,2)]
2×2 Matrix{Matrix{Int64}}:
 [1 5; 2 6] [9 13; 10 14]
 [3 7; 4 8] [11 15; 12 16]

julia> reduce(hcat, reduce.(vcat, eachcol(B)))
4×4 Matrix{Int64}:
 1 5 9 13
 2 6 10 14
 3 7 11 15
 4 8 12 16

```

Although I echo the sentiments above that this is probably an XY problem 🤷‍♂️

---

<div class="post-metadata">

### Author: ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)
#### Post date: [January 10, 2023, 6:37am UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/20 "2023-01-10T06:37:25Z")

</div>

> [@tomerarnon](#):
>
> I would use `@views` here

Yeah, the `@views` macro is definitely more ergonomic here. I wanted to take this opportunity to toy around more with an indexable view object though 😅.

---

<div class="post-metadata">

### Author: ![dfb1002](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfb1002/32/43827_2.png) [@dfb1002](https://discourse.julialang.org/u/dfb1002)
#### Post date: [January 10, 2023, 12:10pm UTC](https://discourse.julialang.org/t/initializing-a-matrix-of-matrices/92743/21 "2023-01-10T12:10:26Z")

</div>

This worked perfectly! Thank you so much! I will keep BlockArrays in mind as well.
