# Replace a specified portion of an array with elements from another smaller array

This may be a trivial question, but I have been at it for a couple of hours and I have not made much progress. I have a 10 x 10 array (T) that I have used to generate an 8 x 8 array (Q). I want to replace the interior of array T with array Q. I can think of lots of really cumbersome ways to do this, but I assume that there is a "good" way to do this...

I suspect that this can be done using ReplaceAll but I cannot get the form correct.

Any pointers would be greatly appreciated.

The code and arrays are:

Nx = 10;
Ny = 10;
(* Initial Temperature Array *)
T = Table[0, {i, Nx}, {j, Ny}];
T[[1]] = Table[Tbottom, {i, Nx}];
T[[Ny]] = Table[Ttop, {i, Nx}];
T[[All, 1]] = Tleft;
T[[All, Nx]] = Tright;
(* Finite Difference of Array *)
Q = Table[
0.25*(T[[i - 1, j]] + T[[i, j + 1]] + T[[i + 1, j]] +
T[[i, j - 1]]), {i, 2, Nx - 1}, {j, 2, Ny - 1}];
(* Output in Matrix Form *)
T // MatrixForm
Q // MatrixForm


Array T $$\left( \begin{array}{cccccccccc} 50 & 50 & 50 & 50 & 50 & 50 & 50 & 50 & 50 & 50 \\ 50 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 50 \\ 50 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 50 \\ 50 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 50 \\ 50 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 50 \\ 50 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 50 \\ 50 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 50 \\ 50 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 50 \\ 50 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 50 \\ 50 & 100 & 100 & 100 & 100 & 100 & 100 & 100 & 100 & 50 \\ \end{array} \right)$$

Array Q $$\left( \begin{array}{cccccccc} 25. & 12.5 & 12.5 & 12.5 & 12.5 & 12.5 & 12.5 & 25. \\ 12.5 & 0. & 0. & 0. & 0. & 0. & 0. & 12.5 \\ 12.5 & 0. & 0. & 0. & 0. & 0. & 0. & 12.5 \\ 12.5 & 0. & 0. & 0. & 0. & 0. & 0. & 12.5 \\ 12.5 & 0. & 0. & 0. & 0. & 0. & 0. & 12.5 \\ 12.5 & 0. & 0. & 0. & 0. & 0. & 0. & 12.5 \\ 12.5 & 0. & 0. & 0. & 0. & 0. & 0. & 12.5 \\ 37.5 & 25. & 25. & 25. & 25. & 25. & 25. & 37.5 \\ \end{array} \right)$$

-

The easiest and most general way is to use a direct assignment on Part, using Span.
First some simpler sample data:

T = ArrayPad[ConstantArray[0, {5, 5}], 1, 1];
Q = Array[Multinomial, {5, 5}];

T // MatrixForm
Q // MatrixForm


Either copy the data to a new symbol as below, or use T directly for in-place modification:

newT = T;

newT[[2 ;; -2, 2 ;; -2]] = Q;

newT


For more examples of the power of this method see:
Elegant operations on matrix rows and columns

-
Thanks so much for the quick reply. This is exactly what I was looking for. Turns out I was sort of close (at one point...) when I tried something like {{2;;10},{2;;10}}. –  navillus5 Mar 14 '13 at 21:10