MathGroup Archive 1996

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

Search the Archive

Re: Setting parts of matrices

  • Subject: [mg2996] Re: Setting parts of matrices
  • From: lk3a at kelvin.seas.virginia.edu (Count Dracula)
  • Date: 19 Jan 1996 11:04:51 -0600
  • Approved: usenet@wri.com
  • Distribution: local
  • Newsgroups: wri.mathgroup
  • Organization: University of Virginia
  • Sender: mj at wri.com

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  


  • Prev by Date: Re: Triangulation Problem
  • Next by Date: Re: Legend on graph with several plots: HOW??
  • Previous by thread: Re: Setting parts of matrices
  • Next by thread: run in background