Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #3452

Re: Trying to use ReplacePart

From Heike Gramberg <heike.gramberg@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Trying to use ReplacePart
Date 2011-07-02 09:05 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iumn0c$abn$1@smc.vnet.net> (permalink)
References <201107010040.UAA24254@smc.vnet.net>

Show all headers | View raw


I don't know if this is more natural, but it does seem to produce the same matrix as your code
without the need to initialise matrix first and with a (in my opinion) more transparent way to select
the elements from matrix1

nNum = 3;
nQnum = 2;

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

matrix1 = RandomInteger[{1, 10}, {2^nNum, 2^nNum}]

list2 = Flatten[Table[(2 j - 1) cNum + i, {j, cQnum/cNum}, {i, cNum}], 1];
matrix = matrix1[[list2, list2]]

Heike.

On 1 Jul 2011, at 01:40, Iv=E1n Lazaro 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}];
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Trying to use ReplacePart Heike Gramberg <heike.gramberg@gmail.com> - 2011-07-02 09:05 +0000

csiph-web