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

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.

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?

1 Like