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


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

Re: Equality...

From A Retey <awnl@gmx-topmail.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Equality...
Date 2012-03-30 09:31 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jl3uh0$3eo$1@smc.vnet.net> (permalink)
References <jl14hh$l4t$1@smc.vnet.net>

Show all headers | View raw


Hi,

>
> I have a matrix with nxn varaibles an entry and want to assign values
> to each of the entries. How can I do this in one go?
>
> I was hoping something like this works, but it doesn't...
>
> Array[Subscript[a, #1, #2]&, {3, 4}] = Array[Random[1], {3, 4}]
>
> Any tips are apprecited.

Set has attribute HoldFirst (for obvious reasons). It does partially 
evaluate its first argument in some cases, but not in this. So you need 
an explicit Evaluate (note that this will only work if no corresponding 
values were defined before):

Evaluate[Array[Subscript[a, #1, #2] &, {3, 4}]] =
  RandomReal[{0, 1}, {3, 4}]

It's not trivial to unset those definitions which you need to do if you 
want to reset those values, here is something I came up with, but I feel 
there must be simpler methods:

ReleaseHold[
  Map[Unset,
   Block[{Subscript},
    Hold @@ {Array[Subscript[a, #1, #2] &, {3, 4}]}], {3}]
  ]

On the other hand, I would suggest to use either:

a = RandomReal[{0, 1}, {3, 4}]

and then access these values with:

a[[i,j]]

or (note that this is one of the cases where the first argument of Set 
is partially evaluated):

Do[a[i,j]=RandomReal[],{i,3},{j,4}]

and:

a[2,3]

which will make your life easier when actually working with these in the 
following code.

hth,

albert

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


Thread

Equality... Roozbeh <rhazrat@gmail.com> - 2012-03-29 07:55 +0000
  Re: Equality... A Retey <awnl@gmx-topmail.de> - 2012-03-30 09:31 +0000

csiph-web