MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Matrix Manipulation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16472] Re: [mg16389] Matrix Manipulation
  • From: BobHanlon at aol.com
  • Date: Sat, 13 Mar 1999 02:22:07 -0500
  • Sender: owner-wri-mathgroup at wolfram.com

In a message dated 3/11/99 4:27:22 PM, bernhard at eng.umd.edu writes:

>I am trying to modify the elements of a submatrix in a matrix, without
>having to replace each element explicitly
>
>for example:
>mm   = { {1,2,3}, {4,5,6}, {7,8,9} }
>msub      = { {21,22}, {23,24} }
>I now wish to add the sub matrix: to positions (1,1),(1,3),(3,1),(3,3)
>
>I have tried using ReplacePart but it does not recognize the
>multidimensional replacement, I have also tried
>mm[ {{1,3} , {1,3}} ]  = mm[ {{1,3} , {1,3}}]  + msub
>
>but this crashes horribly
>

Andy, 

mm   =Array[m, {3,3}];
msub      =Array[s, {2,2}];

For such small matrices, the easiest approach is just to pad msub with 
zeroes and then add to mm.

msub0 = {{s[1, 1], 0, s[1, 2]}, {0,0,0}, 
{s[2, 1], 0, s[2, 2]}};

mm+msub0

{{m[1, 1] + s[1, 1], m[1, 2], m[1, 3] + s[1, 2]}, 
  {m[2, 1], m[2, 2], m[2, 3]}, 
  {m[3, 1] + s[2, 1], m[3, 2], m[3, 3] + s[2, 2]}}

However, for larger matrices

pos = {{1,1}, {1,3}, {3,1}, {3,3}};
msubPos = Transpose[{Flatten[msub], pos}]

{{s[1, 1], {1, 1}}, {s[1, 2], {1, 3}}, {s[2, 1], {3, 1}}, 
  {s[2, 2], {3, 3}}}

Fold[ReplacePart[#1, #1[[Sequence @@ #2[[2]]]]+#2[[1]], #2[[2]]]&, mm, 
  msubPos]

{{m[1, 1] + s[1, 1], m[1, 2], m[1, 3] + s[1, 2]}, 
  {m[2, 1], m[2, 2], m[2, 3]}, 
  {m[3, 1] + s[2, 1], m[3, 2], m[3, 3] + s[2, 2]}}

I'm not sure whether this meets the intent of what you meant by your 
criteria "without having to replace each element explicitly" since this 
uses Fold to step through each replacement.


Bob Hanlon


  • Prev by Date: Programmed PageBreaks
  • Next by Date: ImplicitPlot3D problem
  • Previous by thread: Re: Matrix Manipulation
  • Next by thread: RE: Matrix Manipulation