Re: Replacing Part of a Matrix

*To*: mathgroup at smc.vnet.net*Subject*: [mg20271] Re: [mg20250] Replacing Part of a Matrix*From*: "Tomas Garza" <tgarza at mail.internet.com.mx>*Date*: Sun, 10 Oct 1999 01:27:28 -0400*Sender*: owner-wri-mathgroup at wolfram.com

Wilson Figueroa wrote: > I have a n x n matrix, call it A. > > I have a (n - 2)x(n - 2) matrix, call it B. > > I would like to keep the outer rows and columns > of A, but repalce the inner rows and columns with > thos of B to get a new C. > > I looked at the ReplacePart and other matrix and > list manipulation rules and couldn't quite find > one. > > I wrote a very gross module to do it. > > Does anyone have such a module in Mathematica they would > like to share. > > For example, > > c = replace_a_b[a, b] > > would return the desired result. I think it is easier if you look at your problem in terms of augmenting matrix B with the border elements of A, instead of replacing the inner rows and columns of A with B. Then you can use the following function of A, B and the dimension of A -- assuming it is a square matrix: In[1]:= f[a_, b_, n_] := Partition[ Flatten[{First[a], Transpose[{Drop[Rest[First[Transpose[a]]], -1], Transpose[b], Drop[Rest[Last[Transpose[a]]], -1]}], Last[a]}], n] Using the data in your example: In[2]:= f[a, b, 4] Out[2]= {{1, 2, 3, 4}, {5, 20, 40, 8}, {9, 30, 50, 12}, {13, 14, 15, 16}} Tomas Garza Mexico City