Re: save value in array during loop
- To: mathgroup at smc.vnet.net
- Subject: [mg70497] Re: [mg70487] save value in array during loop
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Wed, 18 Oct 2006 04:16:26 -0400 (EDT)
- References: <200610170659.CAA02132@smc.vnet.net>
On Oct 17, 2006, at 2:59 AM, schaa at geo.uni-koeln.de wrote: > 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? The most basic problem is lists in Mathematica don't start at index 0. Secondly, the format of a For statement is For[start, test, incr, body] so your code needs to be recognized For[i=1,i<=13,i++,rho=(i-1) *25;V[[i]]=UserFunction[0.5]]. In general it's not a good idea to capitalize the names of your variables, it opens the possibility of name collision with predefined objects. An alternative expression is: V=Table[rho=(i-1)*25;UserFunction[0.5],{i,13}] Regards, Ssezi
- References:
- save value in array during loop
- From: schaa@geo.uni-koeln.de
- save value in array during loop