Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

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


  • Prev by Date: Re: sum of binomials .. bug ?
  • 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