Combine two matrices of unequal dimensions
Assuming that you want to insert the 11x11 into the 23x23 at position {6,6}
n = ConstantArray[0, {23, 23}];
m = RandomInteger[1, {11, 11}];
First make a copy of n
if you want to keep it (you could work directly with n
if it was not needed).
newMatrix = n;
Next equate specific elements of newMatrix
with m
.
newMatrix[[6 ;; 16, 6 ;; 16]] = m;
Here is what it looks like
newMatrix // MatrixForm
Since the matrix into which you wish to insert is all zeros you can simply PadRight
the input matrix. I'll use dimensions 5x5 and 11x11 to keep the examples manageable.
SeedRandom[0]
input = RandomInteger[9, {5, 5}]
$\left( \begin{array}{ccccc} 7 & 0 & 8 & 2 & 1 \\ 5 & 8 & 0 & 6 & 7 \\ 2 & 1 & 0 & 6 & 1 \\ 2 & 8 & 6 & 5 & 5 \\ 8 & 4 & 5 & 9 & 0 \\ \end{array} \right)$
This embeds into an 11x11 array starting three rows down and one column in:
PadRight[input, {11, 11}, 0, {3, 1}]
$\left( \begin{array}{ccccccccccc} 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 & 0 \\ 0 & 7 & 0 & 8 & 2 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 5 & 8 & 0 & 6 & 7 & 0 & 0 & 0 & 0 & 0 \\ 0 & 2 & 1 & 0 & 6 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 2 & 8 & 6 & 5 & 5 & 0 & 0 & 0 & 0 & 0 \\ 0 & 8 & 4 & 5 & 9 & 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 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right)$
You can also use ArrayPad
but you'll need to calculate the dimensions:
ArrayPad[input, {{3, 3}, {1, 5}}]
(* same output *)
Here is one way:
n=ConstantArray[0,{23,23}];
m=RandomInteger[1,{11,11}];
With[{i=10,j=10},
ReplacePart[n,Select[
Flatten@MapIndexed[(#2-{6-1,6-1}+{i-1,j-1})->#1&,m,{2}],
AllTrue[First@#,Between[#,{1,23}]&]&
]
]
]//MatrixForm
It puts the element $6,6$ in position $i=10,j=10$.