Re: Replacing Part of a Matrix

*To*: mathgroup at smc.vnet.net*Subject*: [mg20260] Re: Replacing Part of a Matrix*From*: "Allan Hayes" <hay at haystack.demon.co.uk>*Date*: Sun, 10 Oct 1999 00:04:09 -0400*References*: <7tl0bj$40p@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

A = Table[0, {7}, {7}]; B = Table[y, {3}, {3}]; A[[Range[3, 5], Range[3, 5]]] = B; A // MatrixForm 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 y y y 0 0 0 0 y y y 0 0 0 0 y y y 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 This only works with a name (here A) for the matrix to be changed, and it does change the value of A. {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}} [[{2, 3}, {1, 2}]] = {{a, a}, {a, a}}; Set::"setps": "\!\({\(\({0, 0, 0, 0}\)\), \(\({0, 0, 0, 0}\)\), \(\({0, 0, 0, \ 0}\)\), \(\({0, 0, 0, 0}\)\)}\) in assignment of part is not a symbol." A = Table[0, {7}, {7}]; B = Table[y, {3}, {3}]; But we can use a temporay variable (here A1) to avoid these features Block[{A1 = A}, A1[[Range[3, 5], Range[2, 4]]] = B; A1 ] // MatrixForm 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 y y y 0 0 0 0 y y y 0 0 0 0 y y y 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A // MatrixForm 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 0 0 0 0 0 0 0 0 0 0 Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 <flippy652 at my-deja.com> wrote in message news:7tl0bj$40p at smc.vnet.net... > Hello all, > > 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. > > Example: > > A = {{1,2,3,4},{5,6,7,8},{9,10,11,12}, > {13,14,15,16}} > B = {{20,30,},{40,50}} > > C = replace_a_b[A,B], would produce: > > C = {{1,2,3,4},{5,20,30,8},{9,40,50,12}, > {13,14,15,16}} > > Basically, this function just replaces the > interior (n-2)x(n-2) in the larger matrix. > > Can anyone help? > > By the way, thank you to all who responded with > such an easy and useful rectangular grid > generator. > > Thank you. > > Wilson Figueroa > flip at safebunch.com > http://safebunch.com > > > Sent via Deja.com http://www.deja.com/ > Before you buy. >