Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

Re: Changing names of variables in a loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg92199] Re: Changing names of variables in a loop
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Tue, 23 Sep 2008 07:29:30 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <gb7obt$ngj$1@smc.vnet.net>

kazik.lakomy at gmail.com wrote:

> do you know maybe is there a possibility to change a name of a
> variable in a loop? I'm not exactly sure how to explain it, so maybe
> instead of my words I paste a basic example that express my query:
> 
> a1 = 1; a2 = 2; a3 = 3;
> func[g_] := g;
> Summ = 0;
> 
> For[i = 1, i <= 3, i += 1,
>  Summa += func[ai]]
> 
> So something like this... I have tried to deal with this like with
> strings and have put func[a<>"i"] but it didn't work (quite logic).
> The general problem is much more complicated, this is only a part of
> it, so please do not suggest me how can I sum up three numbers, this I
> believe I know :) I would very appreciate any hints,

Use array/function notation: 'someName'['someNumber'] (w/o the quotation 
marks). For instance,

     In[1]:= a[1] = 1; a[2] = 2; a[3] = 3;
             func[g_] := g;
             Summ = 0;
             For[i = 1, i <= 3, i += 1, Summ += func[a[i]]]
             Summ
             Sum[a[i], {i, 1, 3}]

     Out[5]= 6

     Out[6]= 6

Regards,
-- Jean-Marc


  • Prev by Date: Truncated MathPanel Output
  • Next by Date: Re: mathematica 6.0.3 and mac os x 10.5.5 crashes?
  • Previous by thread: Re: Changing names of variables in a loop
  • Next by thread: Re: Changing names of variables in a loop