Mathematica 9 is now available
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: [mg70493] Re: save value in array during loop
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Wed, 18 Oct 2006 04:16:15 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <eh20uf$2nj$1@smc.vnet.net>

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?
> 
> Thanks a lot
> -Ralf
> 
Hi Ralf,

Contrary to some other language like C, Mathematica indices start at 1 
not 0. Therefore, your list V has its first value in V[[1]], its second 
value in V[[2]], etc. Now the tricky part is that V[[0]] exists and is a 
perfectly legal expression; however it does not mean the first element 
of V but the head of the expression V, which is List before being 
replaced. So what you have at the end is not a list/array anymore, but 
another kind of expression. So you must replace your iteration from 0 to 
12 by an iteration from 1 to 13.

In[1]:=
V = Range[13]

Out[1]=
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}

In[2]:=
Head[V]

Out[2]=
List

In[3]:=
V[[0]]

Out[3]=
List

In[4]:=
For[i = 0, i <= 12, rho = i*25;
    V[[i]] = UserFunction[0.5 + i]; i++]

In[5]:=
V

Out[5]=
UserFunction[0.5][UserFunction[1.5],
   UserFunction[2.5], UserFunction[3.5],
   UserFunction[4.5], UserFunction[5.5],
   UserFunction[6.5], UserFunction[7.5],
   UserFunction[8.5], UserFunction[9.5],
   UserFunction[10.5], UserFunction[11.5],
   UserFunction[12.5], 13]

In[6]:=
Head[V]

Out[6]=
UserFunction[0.5]

In[7]:=
V[[0]]

Out[7]=
UserFunction[0.5]

In[8]:=
V = Range[13]

Out[8]=
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}

In[9]:=
For[i = 1, i <= 13, rho = i*25;
    V[[i]] = UserFunction[0.5 + i]; i++]

In[10]:=
Information["V", LongForm -> False]

V

V = {UserFunction[1.5], UserFunction[2.5],
      UserFunction[3.5], UserFunction[4.5],
      UserFunction[5.5], UserFunction[6.5],
      UserFunction[7.5], UserFunction[8.5],
      UserFunction[9.5], UserFunction[10.5],
      UserFunction[11.5], UserFunction[12.5],
      UserFunction[13.5]}

Regards,
Jean-Marc


  • Prev by Date: RE: Re: Functions inside list
  • Next by Date: General--Mathematica is unable to yield result from LinearSolve with good Preci
  • Previous by thread: Re: save value in array during loop
  • Next by thread: Dimensional analysis package