# Matrix inside Matrix in Julia

**URL:** <https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280>\
**Category:** General Usage\
**Tags:** matrix\
**Created:** [April 20, 2024, 9:23pm UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280 "2024-04-20T21:23:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sati/32/206266_2.png) [@sati](https://discourse.julialang.org/u/sati)\
**Post date:** [April 20, 2024, 9:23pm UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280/1 "2024-04-20T21:23:21Z")

</div>

This is a matrix formula derived from algebra. My question is how to implement this in Julia?

 ![matrixMJulia](https://global.discourse-cdn.com/julialang/original/3X/4/1/41bed75059b1c1b639a8dfb36fb3552e019266c1.jpeg)

For the first row : I is an identity matrix with a size 3x3 , 0 is a zero matrix with a size 3x6, AG is a 3x10 matrix, and IGR is a 3x1 matrix.  
For the second row: I is an identity matrix with a size 6x6 , 0 is a zero matrix with a size 6x3, AD is a 6x10 matrix, and IDR is a 6x1 matrix.

and other rows are likewise.  
Thanks

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [April 20, 2024, 9:40pm UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280/2 "2024-04-20T21:40:16Z")

</div>

Some zero elements do not specify a dimension. Can you clarify whether those are scalars or zero matrices of size 6X3?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [April 20, 2024, 10:42pm UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280/3 "2024-04-20T22:42:29Z")

</div>

If the matrix blocks are not too large, implementing this using Arrays / Matrix types of Julia would be easiest. A different representation would be to use BlockArrays:

```julia
using LinearAlgebra, FillArrays, BlockArrays

AG, AD = rand(6,10), rand(3,10)
TL = mortar((I(3), Fill(0.0,3,6), -AG),(Fill(0.0,6,3),-I(6),-AD))
M = mortar((TL,Fill(0.0,9,19)),(Fill(0.0,9,19),TL))

IGR, IDR, ILR, IG2, ID2, IL2 = 
  rand(3), rand(6), rand(10), rand(3), rand(6), rand(10)
V = mortar([mortar([IGR, IDR, ILR]), mortar([IG2, ID2, IL2])])

```

with these defined, the matrix product in the diagram can be calculated as:

```julia
julia> M*V
2-blocked 18-element BlockVector{Float64}:
 -2.7166482252406086
 -0.7411532464031856
 -1.4144505807049303
 -3.9973523077216147
 -4.2554463973398535
 -3.9794920828563947
 -3.6925883969453324
 -3.286901004924977 
 -1.6640267390765626
 ───────────────────
 -2.899615642942856 
 -1.873631050122329 
 -1.477001012721034 
 -3.3983276831541374
 -3.1006965784125096
 -4.0077663400711785
 -3.2353523978568464
 -3.3306260239290744
 -1.9377803558173756

```

It would be necessary to learn the other featuers of BlockedArrays (such as block indexing etc.) to really get the benefits.

---

<div class="post-metadata">

**Author:** ![sati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sati/32/206266_2.png) [@sati](https://discourse.julialang.org/u/sati)\
**Post date:** [April 21, 2024, 12:23am UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280/4 "2024-04-21T00:23:48Z")

</div>

all of them are matrices. do we need to specify their dimension one by one or Julia recognizes the size of zeros and identity matrices?

---

<div class="post-metadata">

**Author:** ![sati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sati/32/206266_2.png) [@sati](https://discourse.julialang.org/u/sati)\
**Post date:** [April 21, 2024, 12:27am UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280/5 "2024-04-21T00:27:45Z")

</div>

thanks.  
IGR, IDR, ILR, IGI, IDI, ILI are variables.  
Also, did you consider other zero matrices in M?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 21, 2024, 8:54am UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280/6 "2024-04-21T08:54:57Z")

</div>

you could try the cat() function in its various forms

```julia
using LinearAlgebra

FI=I(3)*1
SI=I(6)*(-1)

FO=zeros(Int,3,6)
SO=zeros(Int,6,3)

AG=rand(0:9,3,10)
AD=rand(0:9,6,10)

FB=hcat(FI,FO,AG)
SB=hcat(SI,SO,AD)

M1=vcat(FB,SB)

cat(M1,M1,dims=(1,2))

```

If you only care about the result, you could rewrite the product like this (equivalent)

```julia
[M1*v1;M1*v2]

```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [April 21, 2024, 10:25am UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280/7 "2024-04-21T10:25:54Z")

</div>

I’m fairly certain that you would have to specify the dimensions of your zero matrices. There might be a way to setup your solution so that zeros are used by default, and the indices of non-zero elements are specified manually.

One thing that might help is to compare the solutions offered by Dan and rocco\_sprmnt21 with a simplified example. If you identify any limitations in the processes, we might be able to offer additional help.

---

<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:** [April 21, 2024, 11:46am UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280/8 "2024-04-21T11:46:55Z")

</div>

> [@rocco\_sprmnt21](#):
>
> you could try the cat() function in its various forms

It’s even easier just to use the literal matrix constructor `[...]`, which implicitly calls [`hvcat`](https://docs.julialang.org/en/v1/base/arrays/#Base.hvcat):

```julia
using LinearAlgebra # for I
AG=rand(0:9,3,10)
AD=rand(0:9,6,10)

M = [ I zeros(3,6) -AG
     zeros(6,3) -I -AD]
A = [ M zero(M)
     zero(M) M]

```

Note that `hvcat` automatically figures out the sizes of _square_ blocks involving `I` or multiples thereof, so I don’t even need to specify the size of the `I` terms (though I could use `I(3)` and `I(6)` to give these explicitly). And `zero(M)` is a matrix of zeros the same size as `M` (the additive identity for matrices of this size). (I initially used `0I` here, but `[M 0I; 0I M]` makes _square_ blocks of zeros, which is not what you seem to want.)

I would say that this is the most idiomatic way to build a matrix out of blocks in Julia (similar to Matlab, though Matlab doesn’t have our implicit `I`).

A similar construction also works with sparse arrays. It’s probably not worth it here for performance on such a small array with dense blocks, but it does give a nice visualization of the matrix:

```julia
julia> using SparseArrays

julia> sparse(A)
18×38 SparseMatrixCSC{Float64, Int64} with 180 stored entries:
⎡⠑⢄⠀⠀⢸⣿⣻⣿⣾⡃⠀⠀⠀⠀⠀⠀⠀⠀⠀⎤
⎢⠀⠀⠑⢄⢨⣟⣽⡿⣯⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⎥
⎢⠀⠀⠀⠀⠉⠉⠈⠉⠉⠑⢄⠀⠀⠀⣶⡶⣶⣦⡶⎥
⎢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⢄⠀⣻⢟⣿⣿⣻⎥
⎣⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠑⠛⠋⠛⠚⠛⎦

```

If you are looking for the vectors v where Av = 0 as in your formula, you can use [`nullspace(A)`](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.nullspace). Here, your A matrix is 18 \times 38 with [rank](https://en.wikipedia.org/wiki/Rank_(linear_algebra)) 18, so it has a [20](https://en.wikipedia.org/wiki/Rank%E2%80%93nullity_theorem)-dimensional [nullspace](https://en.wikipedia.org/wiki/Kernel_(linear_algebra)) — there are 20 independent possible vectors (and linear combinations thereof) that satisfy your equation, and the columns of `nullspace(A)` are an orthonormal basis for this space.

---

<div class="post-metadata">

**Author:** ![sati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sati/32/206266_2.png) [@sati](https://discourse.julialang.org/u/sati)\
**Post date:** [April 25, 2024, 4:31pm UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280/9 "2024-04-25T16:31:21Z")

</div>

Thank guys, I tried both Dan and Steven code and worked. Since I’m new to this type of codes, could you also please help me with implementing the following as well?  
Thanks.

---

<div class="post-metadata">

**Author:** ![sati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sati/32/206266_2.png) [@sati](https://discourse.julialang.org/u/sati)\
**Post date:** [April 25, 2024, 3:36pm UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280/10 "2024-04-25T15:36:49Z")

</div>

These are two matrix formulas derived from algebra. My question is how to implement this in Julia?

 ![Q2_2024-04-25_11-23-16](https://global.discourse-cdn.com/julialang/original/3X/3/6/36db95f472dc32e0d3ed6acd12a0100d839b8164.jpeg)

1. In the first matrix:  
tg(theta1) is tangent of theta1 and is a 6x1 matrix, tg(theta2) is tangent of theta2 and is a 6x1 matrix, I is an identity matrix ,  
IGR, IDR, ILR, IGI, IDI, ILI are variables. (IGR is a 3x1 matrix, IDR is a 6x1 matrix, ILR is a 10x1 matrix, IGI is a 3x1 matrix, IDI is a 6x1 matrix, ILI is a 10x1 matrix)  
uGR1, uGR2, uGI1,uGI2 are 3x1 matrices.  
other elements are zero matrices.

2. In the second matrix:  
tg(theta1) is tangent of theta1 and is a 6x1 matrix, tg(theta2) is tangent of theta2 and is a 6x1 matrix, I is an identity matrix , m is a 6x1 matrix.  
VGR, VDR, VGI, VDI are variables. (VGR is a 3x1 matrix, VDR is a 6x1 matrix, VGI is a 3x1 matrix, VDI is a 6x1 matrix.  
other elements are zero matrices.

Thanks.

---

<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:** [April 25, 2024, 5:09pm UTC](https://discourse.julialang.org/t/matrix-inside-matrix-in-julia/113280/11 "2024-04-25T17:09:53Z")

</div>

We already gave you sample code for your other example. This example looks very similar. You should try to _understand_ how the provided sample code works, and then apply similar principles to your new problem.

If you have a _specific_ confusion about the examples or explanations above, feel free to ask. But you should make an effort to learn Julia and write your own code, not simply ask people to write more code for you.
