Re: Did something change?
- To: mathgroup at smc.vnet.net
- Subject: [mg58122] Re: [mg58109] Did something change?
- From: Andrzej Kozlowski <andrzej at akikoz.net>
- Date: Sun, 19 Jun 2005 03:43:25 -0400 (EDT)
- References: <200506181008.GAA08878@smc.vnet.net> <A5BD3CF8-FDED-4DAA-8E0F-A0CC0B8ADD02@akikoz.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 18 Jun 2005, at 22:00, Andrzej Kozlowski wrote: > > On 18 Jun 2005, at 19:08, Ronald Bruck wrote: > > >> >> Recently I created a list of several matrices, something like >> >> Do[F[i] = IdentityMatrix[8]; F[i][[1,3]] = 7, {i,10}] >> >> and was surprised to get the message that F[i] wasn't a symbol. I'm >> pretty sure code similar to that worked prior to Mathematica 5. Did >> something change? >> >> I've also tried Symbolize[F[i]] in that loop, but it doesn't help; >> Mathematica refuses to access part [[1,3]] of F[i], claiming F[i] >> isn't >> a symbol. >> >> How are you supposed to set the individual values of a matrix if you >> can't do this? This is an EXTREMELY unhelpful "feature". >> >> --Ron Bruck >> >> >> > > > This is the way Set and Part work in Mathematica work and always > have done. One way to deal with this type of problem is to use > ReplacePart instead of Part: > > Do[F[i] = IdentityMatrix[8]; F[i] = ReplacePart[F[i], 7, {1, 3}], > {i, 10}] > > or, if you really want to use Set and Part as in your original > example you have to introduce a temporary symbol, e.g.: > > Do[F[i] = IdentityMatrix[8]; Block[{B = F[i]}, B[[1, 3]] = > 7; F[i] = B], {i, 10}] > > > Andrzej Kozlowski > > Chiba,Japan > > I forgot to say that a much better approach than the above is to give up using Do altogether and instead use a functional approach: F = Table[ IdentityMatrix[8], {i, 10}]; F[[All, 1, 3]] = 7; Now you have F[[1]] {{1, 0, 7, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 1}} and so on. ANdrzej
- References:
- Did something change?
- From: Ronald Bruck <bruck@math.usc.edu>
- Did something change?