Re: save value in array during loop
- To: mathgroup at smc.vnet.net
- Subject: [mg70520] Re: save value in array during loop
- From: Bill Rowe <readnewsciv at sbcglobal.net>
- Date: Wed, 18 Oct 2006 04:18:03 -0400 (EDT)
On 10/17/06 at 2:59 AM, schaa at geo.uni-koeln.de wrote: >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? For starters, Mathematica does not use 0 as the starting index for arrays. So, if V is a 13 element array your code needs to be: For[i=1,i<=13, rho = (i-1)*25; V[[i]] = UserFunction[0.5]; i++] But this isn't a very efficient way to do things in Mathematica. As written, it appears you are assigning every element of V the same value, UserFunction[0.5]. A more efficient way to do this would be Table[V[[i]]=UserFunction[0.5],{i,13}] Now assuming you actually intended the argument to UserFunction to be 0.5*rho (why compute a value for rho if it is never used?) and UserFunction has the attribute listable. V=UserFunction[.5 Range[0, 300,25]] or if UserFunction doesn't have the attribute listable V = UserFunction/@(.5 Range[0, 300, 25]) -- To reply via email subtract one hundred and four