MathGroup Archive 2011

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

Search the Archive

Trying to use ReplacePart

  • To: mathgroup at
  • Subject: [mg119943] Trying to use ReplacePart
  • From: Iván Lazaro <gaminster at>
  • Date: Thu, 30 Jun 2011 20:40:45 -0400 (EDT)


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

  • Prev by Date: Re: Bug in Sum?
  • Next by Date: Running notebook from command line
  • Previous by thread: Why does Solve give me no solutions for this in Version 8.0.1?
  • Next by thread: Running notebook from command line