MathGroup Archive 1996

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

Search the Archive

Re: Setting parts of matrices

  • To: mathgroup at smc.vnet.net
  • Subject: [mg2996] Re: Setting parts of matrices
  • From: Count Dracula <lk3a at kelvin.seas.virginia.edu>
  • Date: Fri, 19 Jan 1996 02:27:45 -0500
  • Organization: University of Virginia

In article <4cqk0q$ig5 at dragonfly.wri.com>, jnygaard at math.uio.no writes:
>
>Can somebody tell me how to set a submatrix of a larger matrix?
>Assume a is a larger matrix than b. I would like to do something
>like
>
>  a[[Range[i1,i2],Range[j1,j2]]]=b;
>
>Unfortunately, this does not work

A solution to this was posted some time ago by Alan Hayes.
It was similar to the MatrixInsert module below. 
MatrixInsert[A, B, rowbeg, colbeg] inserts matrix B into matrix A
with the (1, 1) element of B replacing the (rowbeg, colbeg) element 
of A. The module returns Null but changes A.

Attributes[MatrixInsert] = {HoldFirst}

MatrixInsert[A_?MatrixQ, B_?MatrixQ, rowbeg_Integer, colbeg_Integer] := 
    Module[ {rowend, colend, aux},
      {rowend, colend} = {rowbeg, colbeg} + Dimensions[B] - 1; 
      aux = Take[A, {rowbeg, rowend}];
      A = Join[
               Take[A, rowbeg - 1],
               MapThread[Join, {Map[Take[#1, colbeg - 1] &, aux], B, Map[Drop[#1, colend] &, aux]}],
               Take[A, rowend - Length[A]]
              ];
                ]

A = IdentityMatrix[5];

B = {{a, b}, {c, d}};

test := (MatrixInsert[A, B, 2, 4]; MatrixForm[A]);


The test run gives this:

In[2]:= test

Out[2]//MatrixForm= 1   0   0   0   0

                    0   1   0   a   b

                    0   0   1   c   d

                    0   0   0   1   0

                    0   0   0   0   1

-- 
___________________________________________________________________________________
Levent Kitis           lk3a at cars.mech.virginia.edu    lk3a at kelvin.seas.virginia.edu
University of Virginia Department of Mechanical, Aerospace, and Nuclear Engineering  

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Re: Triangulation Problem
  • Next by Date: Re: Triangulation Problem
  • Previous by thread: Re: Setting parts of matrices
  • Next by thread: Re: Setting parts of matrices