Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

Re: Trying to use ReplacePart

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119958] Re: Trying to use ReplacePart
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Fri, 1 Jul 2011 04:50:37 -0400 (EDT)

On 6/30/11 at 8:40 PM, gaminster at gmail.com (Iv=C3=A1n Lazaro) wrote:

>I needed some days ago to replace elements in a matrix. My first
>thought was to use ReplacePart, but I found myself trying to tell it
>to make all replacements at once, instead of returning multiple
>copies of the original matrix with only one element changed. As I
>was in a hurry, I solved the problem in a different way. However I'm
>still wondering how can I use a more "natural" code to solve this
>problem.

>nNum = 3; nQnum = 2;

>cNum = 2^(nNum - nQnum); cQnum = 2^(nNum - 1);

>list = Table[i, {i, 1, cQnum}]; list1 = Partition[list, cNum];
>matrix = ConstantArray[0, {cQnum, cQnum}]; matrix1 =
>RandomInteger[{1, 10}, {2^nNum, 2^nNum}]

>(matrix[[#[[1]], #[[2]]]] =
>matrix1[[
>cNum*Position[list1, #[[1]]][[1, 1]] + #[[1]], cNum*Position[list1,
>#[[2]]][[1, 1]] + #[[2]]]]) & /@ Tuples[{list, list}];

>or a faster solution,

>Table[matrix[[i, j]] =
>matrix1[[
>cNum*Position[list1, i][[1, 1]] + i, cNum*Position[list1, j][[1, 1]]
>+ j]], {i, 1, cQnum}, {j, 1,cQnum}];

Natural is not well defined. But simpler I can do. First, I can
generate a list of the rows/cols you appear to be after as follows:

In[3]:= rcList = Flatten@Partition[Range[3, 8], 2, 4]

Out[3]= {3,4,7,8}

with that I can create a 4 x 4 matrix with entries from the
larger matrix as follows:

In[4]:= lgMatrix = Table[a[i, j], {i, 8}, {j, 8}];

In[5]:= smMatrix = lgMatrix[[rcList, rcList]]

Out[9]= {{a[3, 3], a[3, 4], a[3, 7], a[3, 8]}, {a[4, 3], a[4, 4],
      a[4, 7], a[4, 8]}, {a[7, 3], a[7, 4], a[7, 7], a[7, 8]},
    {a[8, 3], a[8, 4], a[8, 7], a[8, 8]}}



  • Prev by Date: Re: Improt vs Get
  • Next by Date: Re: Remote presentation clicker for mathematica slideshows.
  • Previous by thread: Re: Improt vs Get
  • Next by thread: Re: Trying to use ReplacePart