MathGroup Archive 2006

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

Search the Archive

Re: fast replace for matrix minor

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69511] Re: fast replace for matrix minor
  • From: "astanoff" <astanoff at gmail.com>
  • Date: Thu, 14 Sep 2006 06:56:30 -0400 (EDT)
  • References: <ee8hcp$j7g$1@smc.vnet.net>

Christopher Arthur wrote:
> Is there a quick way to replace a small minor block in a large matrix?
> Using ReplacePart in a Do loop seems to be very inefficient with
> respect to timing
>
> Chris Arthur

Hi Chris,

This is my way to do that kind of block replace
without any Do, For or While :

In[1]:=replaceBlock[oldMatrix_?MatrixQ, newBlock_?MatrixQ,
      {startRow_Integer, startColumn_Integer}]:=
    Module[{endRow, endColumn, rep},
      {endRow, endColumn} = {startRow, startColumn}+
	Dimensions[newBlock]-1;
      rep[i_ /; startRow <= i <= endRow,
          j_ /; startColumn <= j <= endColumn]:=
        newBlock[[i-startRow+1, j-startColumn+1]];
      rep[i_,j_] := oldMatrix[[i,j]];
      Array[rep, Dimensions[oldMatrix]]
    ];

Example :

In[6]:=replaceBlock[Array[x,{10,5}],Array[y,{2,4}],{3,2}]

Out[6]={{x[1,1],x[1,2],x[1,3],x[1,4],x[1,5]},
{x[2,1],x[2,2],x[2,3],x[2,4],x[2,5]},
{x[3,1],y[1,1],y[1,2],y[1,3],y[1,4]},
{x[4,1],y[2,1],y[2,2],y[2,3],y[2,4]},
{x[5,1],x[5,2],x[5,3],x[5,4],x[5,5]},
{x[6,1],x[6,2],x[6,3],x[6,4],x[6,5]},
{x[7,1],x[7,2],x[7,3],x[7,4],x[7,5]},
{x[8,1],x[8,2],x[8,3],x[8,4],x[8,5]},
{x[9,1],x[9,2],x[9,3],x[9,4],x[9,5]},
{x[10,1],x[10,2],x[10,3],x[10,4],x[10,5]}}

hth [you to improve your timing...]

Valeri


  • Prev by Date: Integate[] warning shows only first time, but not the second time?
  • Next by Date: RE: question on changing 'type' of numbers
  • Previous by thread: Re: fast replace for matrix minor
  • Next by thread: Differential equation