# Nested array replacement

**URL:** <https://discourse.julialang.org/t/nested-array-replacement/102348>\
**Category:** New to Julia\
**Tags:** array, reshaping\
**Created:** [August 1, 2023, 11:36am UTC](https://discourse.julialang.org/t/nested-array-replacement/102348 "2023-08-01T11:36:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Denis\_Ivanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denis_ivanov/32/52607_2.png) [@Denis\_Ivanov](https://discourse.julialang.org/u/Denis_Ivanov)\
**Post date:** [August 1, 2023, 11:36am UTC](https://discourse.julialang.org/t/nested-array-replacement/102348/1 "2023-08-01T11:36:15Z")

</div>

Hi! I would like to model this behavior on Julia: [Substitution System](https://wolfram.com/xid/09a8lh2zm-pv6wsv)  
So if we have rules `( 1 => [[1, 0], [1, 1]], 0 => [[0, 0], [0, 0]] )`  
and array `[[1, 0], [1, 1]]` than output should be `[1, 0, 0, 0], [1, 1, 0, 0], [1, 0, 1, 0], [1, 1, 1, 1]]`.  
How to do it with `replacement` and `reshape`?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [August 1, 2023, 12:22pm UTC](https://discourse.julialang.org/t/nested-array-replacement/102348/2 "2023-08-01T12:22:07Z")

</div>

It is a little easier if you use 2D arrays instead of vectors of vectors.

```julia
rules = Dict([1 => [1 0; 1 1], 0 => [0 0; 0 0]])

initial = [1 0; 1 1]

apply(rules, state) = reduce(
    vcat,
    map(x -> reduce(hcat, x), map(x -> getindex.(Ref(rules), x), eachrow(state))),
)

apply(rules, initial)

```

---

<div class="post-metadata">

**Author:** ![Denis\_Ivanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denis_ivanov/32/52607_2.png) [@Denis\_Ivanov](https://discourse.julialang.org/u/Denis_Ivanov)\
**Post date:** [August 1, 2023, 1:15pm UTC](https://discourse.julialang.org/t/nested-array-replacement/102348/3 "2023-08-01T13:15:08Z")

</div>

Thank you, cool!

---

<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:** [August 2, 2023, 8:38pm UTC](https://discourse.julialang.org/t/nested-array-replacement/102348/4 "2023-08-02T20:38:54Z")

</div>

```julia
julia> dm=Dict( 1 => [1 0; 1 1], 0 => [0 0; 0 0] )
Dict{Int64, Matrix{Int64}} with 2 entries:
  0 => [0 0; 0 0]
  1 => [1 0; 1 1]

julia> k=[1, 1, 0, 1]
4-element Vector{Int64}:
 1
 1
 0
 1

julia> using BlockArrays

julia> bl=get.([dm],k,missing)
4-element Vector{Matrix{Int64}}:
 [1 0; 1 1]
 [1 0; 1 1]
 [0 0; 0 0]
 [1 0; 1 1]

julia> mb=mortar(reshape(bl,2,2))
2×2-blocked 4×4 BlockMatrix{Int64}:
 1 0 │ 0 0
 1 1 │ 0 0
 ──────┼──────
 1 0 │ 1 0
 1 1 │ 1 1

julia> Matrix(mb)
4×4 Matrix{Int64}:
 1 0 0 0
 1 1 0 0
 1 0 1 0
 1 1 1 1

```

---

<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:** [August 2, 2023, 8:40pm UTC](https://discourse.julialang.org/t/nested-array-replacement/102348/5 "2023-08-02T20:40:57Z")

</div>

```julia
julia> d=Dict( 1 => [[1, 0], [1, 1]], 0 => [[0, 0], [0, 0]] )
Dict{Int64, Vector{Vector{Int64}}} with 2 entries:
  0 => [[0, 0], [0, 0]]
  1 => [[1, 0], [1, 1]]

julia> k=[[1, 0], [1, 1]]
2-element Vector{Vector{Int64}}:
 [1, 0]
 [1, 1]

julia> idx=permutedims(Tuple.(CartesianIndices((2,2,2))),(3, 1, 2))[:]
8-element Vector{Tuple{Int64, Int64, Int64}}:
 (1, 1, 1)
 (1, 1, 2)
 (2, 1, 1)
 (2, 1, 2)
 (1, 2, 1)
 (1, 2, 2)
 (2, 2, 1)
 (2, 2, 2)

julia> ridx=reshape(idx,4,2)
4×2 Matrix{Tuple{Int64, Int64, Int64}}:
 (1, 1, 1) (1, 2, 1)
 (1, 1, 2) (1, 2, 2)
 (2, 1, 1) (2, 2, 1)
 (2, 1, 2) (2, 2, 2)

julia> m=[d[k[x][y]][z] for (x,y,z) in ridx]
4×2 Matrix{Vector{Int64}}:
 [1, 0] [0, 0]
 [1, 1] [0, 0]
 [1, 0] [1, 0]
 [1, 1] [1, 1]

julia> [vcat(r...) for r in eachrow(m)]
4-element Vector{Vector{Int64}}:
 [1, 0, 0, 0]
 [1, 1, 0, 0]
 [1, 0, 1, 0]
 [1, 1, 1, 1]

```
