Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Trying to use ReplacePart
Replies: 3   Last Post: Jul 6, 2011 4:54 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Dana DeLouis

Posts: 107
Registered: 12/14/08
Re: Trying to use ReplacePart
Posted: Jul 5, 2011 4:24 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hi. Just to add...if you change your inputs, then things might get messy.
Not sure, but does this help?

Here is your example:

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}];


you=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}]

{{6,3,1,1},{4,6,6,1},{9,1,2,3},{6,3,10,4}}

Here's another idea:

r=Table[cNum+j+cNum*Floor[(j-1)/cNum],{j,cQnum}]
{3,4,7,8}

me=Partition[Extract[matrix1,Tuples[r,2]],cQnum]
{{6,3,1,1},{4,6,6,1},{9,1,2,3},{6,3,10,4}}

you==me
True

= = = = = =

I=92ll do it again with different starting numbers (ie nNum and nQnum)

nNum=5;
nQnum=3;
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}];


you=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}];

Just note that your solution returns a 16 by 16 array:

Dimensions[you]
{16,16}

I'll try my version again:

r=Table[cNum+j+cNum*Floor[(j-1)/cNum],{j,cQnum}]
{5,6,7,8,13,14,15,16,21,22,23,24,29,30,31,32}

me=Partition[Extract[matrix1,Tuples[r,2]],cQnum];

me==you
True

Hmmm. Appears to work! :>)

= = = = = = = = = =
HTH : >)
Dana DeLouis
$Version
8.0 for Mac OS X x86 (64-bit) (November 6, 2010)





On Jun 30, 8:43 pm, Iv=E1n Lazaro <gamins...@gmail.com> wrote:
> Hi!
>
> 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}];


Re: Trying to use ReplacePart=




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.