Re: Replacing Part of a Matrix

• To: mathgroup at smc.vnet.net
• Subject: [mg20267] Re: [mg20250] Replacing Part of a Matrix
• From: "Andrzej Kozlowski" <andrzej at tuins.ac.jp>
• Date: Sun, 10 Oct 1999 01:27:26 -0400
• Sender: owner-wri-mathgroup at wolfram.com

```Here is, I think, the simplest way to do this:

In[1]:=
replaceInterior[a_, b_] :=
Module[{c = a, l = Length[a]}, c[[{2, l - 1}, {2, l - 1}]] = b; c]

Now, with your definitions

In[1]:=
(a = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12},
{13, 14, 15, 16}});

In[2]:=
(b = {{20, 30}, {40, 50}});

In[3]:=
replaceInterior[a, b]
Out[3]=
{{1, 2, 3, 4}, {5, 20, 30, 8}, {9, 40, 50, 12}, {13, 14, 15, 16}}
--
Andrzej Kozlowski
Toyama International University
JAPAN
http://sigma.tuins.ac.jp
http://eri2.tuins.ac.jp

----------
>From: flippy652 at my-deja.com
>To: mathgroup at smc.vnet.net
>Subject: [mg20267] [mg20250] Replacing Part of a Matrix
>Date: Sat, Oct 9, 1999, 7:30
>

> 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.
>

```

• Prev by Date: Re: Finding lengths of identical sequences in lists
• Next by Date: Re: Replacing Part of a Matrix
• Previous by thread: Re: Replacing Part of a Matrix
• Next by thread: Re: Replacing Part of a Matrix