Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #9266 > unrolled thread
| Started by | Roozbeh <rhazrat@gmail.com> |
|---|---|
| First post | 2012-03-29 07:55 +0000 |
| Last post | 2012-03-30 09:31 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
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
| From | Roozbeh <rhazrat@gmail.com> |
|---|---|
| Date | 2012-03-29 07:55 +0000 |
| Subject | Equality... |
| Message-ID | <jl14hh$l4t$1@smc.vnet.net> |
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.
R
[toc] | [next] | [standalone]
| From | A Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2012-03-30 09:31 +0000 |
| Message-ID | <jl3uh0$3eo$1@smc.vnet.net> |
| In reply to | #9266 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web