The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Trying to use ReplacePart
Replies: 1   Last Post: Jul 1, 2011 4:03 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Iván Lazaro

Posts: 34
Registered: 6/18/07
Trying to use ReplacePart
Posted: Jun 30, 2011 8:00 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply


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]]]] =
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]] =
cNum*Position[list1, i][[1, 1]] + i, cNum*Position[list1, j][[1, 1]] +
j]], {i, 1, cQnum}, {j, 1,cQnum}];

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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.