[Date Index]
[Thread Index]
[Author Index]
Re: save value in array during loop
*To*: mathgroup at smc.vnet.net
*Subject*: [mg70513] Re: save value in array during loop
*From*: Peter Pein <petsie at dordos.net>
*Date*: Wed, 18 Oct 2006 04:17:38 -0400 (EDT)
*References*: <eh20uf$2nj$1@smc.vnet.net>
schaa at geo.uni-koeln.de schrieb:
> Hi there,
>
> I am new to mathematica and I have the following problem:
>
> save a certain value in an array calculated during a loop.
>
> My attempt:
>
> For[i=0,i<=12,
> rho = i*25;
> V[[i]] = UserFunction[0.5];
> i++]
>
> rho has the role of a global variable and is used in several other functions.
> UserFunction stands for a function which in turn calls other functions.
> V is the array of dimension 13 from 0 to 12.
>
> The code above does not work. What needed to be changed to make it
> work?
>
> Thanks a lot
> -Ralf
>
Hi Ralf,
1.) V is used as the name of a function in the Combinatorica-Package. If you
don't use that, it _might_ be ok, to use a capital V as variable.
2.) if v has not been defined before, v[[i]] references nothing.
2.) The first element of a list is the first ;-), not the one with index zero.
v[[0]] returns the "Head" of v.
3.) the use of global variables leads often to undesired side-effects.
4.) try to avoid For, Do etc.
v=Table[rho=25 i; UserFunction[1/2], {i,0,12}]
or
v=(rho=25#; UserFunction[1/2])&/@Range[0,12]
or
v=Array[rho=25 i; UserFunction[1/2]&,13,0]
but if you insist on using For, you have to build the list v before entering
the loop and using Mathematicas order For[init,test,incr,body] should help too:
v=Range[13];
For[i=0, i<=12, i++, rho=25 i; v[[i+1]]=UserFunction[1/2]];
Clear[i];
you see: it is very complicated to use For[] in Mathematica.
Peter
Prev by Date:
**Re: Graphics/PlotRange/LaTeX/Psfrag**
Next by Date:
**Re: Literal HTML**
Previous by thread:
**Re: save value in array during loop**
Next by thread:
**Re: save value in array during loop**
| |