# Pluggin a series of small matrices into a larger matrix (with addition)

**URL:** <https://discourse.julialang.org/t/pluggin-a-series-of-small-matrices-into-a-larger-matrix-with-addition/74255>\
**Category:** New to Julia\
**Tags:** matrices, cartesianindices\
**Created:** [January 8, 2022, 8:39pm UTC](https://discourse.julialang.org/t/pluggin-a-series-of-small-matrices-into-a-larger-matrix-with-addition/74255 "2022-01-08T20:39:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![drew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drew/32/34017_2.png) [@drew](https://discourse.julialang.org/u/drew)\
**Post date:** [January 8, 2022, 8:39pm UTC](https://discourse.julialang.org/t/pluggin-a-series-of-small-matrices-into-a-larger-matrix-with-addition/74255/1 "2022-01-08T20:39:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 8, 2022, 8:51pm UTC](https://discourse.julialang.org/t/pluggin-a-series-of-small-matrices-into-a-larger-matrix-with-addition/74255/2 "2022-01-08T20:51:30Z")

</div>

```julia
julia> a
4×4 Matrix{Int64}:
 0 0 0 0
 0 0 0 0
 0 0 0 0
 0 0 0 0

julia> for i in 1:3
           window = CartesianIndices((i:i+1, i:i+1))
           a[window] .+= i
       end;

julia> a
4×4 Matrix{Int64}:
 1 1 0 0
 1 3 2 0
 0 2 5 3
 0 0 3 3

```

you have a typo, the `4` should be `5` because 2+3 right?
