Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1992
*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 1992

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

Search the Archive

Re: Table[ ...If[...i...] ,{i,...}]

  • To: mathgroup at yoda.physics.unc.edu
  • Subject: Re: Table[ ...If[...i...] ,{i,...}]
  • From: David Withoff <withoff>
  • Date: Fri, 17 Jul 1992 10:21:33 -0500

> Hi mathgroup,
>  why is the i in
>
>  Table[ v[i]'[t] == If[ x[i]>0,ex1,ex2] , {i,2,6} ]
> 
>  not replaced with the iterator i?
>   thanks
> 
>   michael trott
> 
>   trott at physik.th-ilmenau.de

The Table function works by sequentially assigning values to the
iteration parameter, evaluating the first argument, and using the
result to fill in the table.  In the above example it constructs
a list with five elements:

In[1]:= result = {Null, Null, Null, Null, Null}

assigns a value to i:

In[2]:= i = 2

evaluates the first argument:

In[3]:= v[i]'[t] == If[ x[i]>0,ex1,ex2]

Out[3]= (v[2])'[t] == If[x[i] > 0, ex1, ex2]

fills in the first entry of the table:

In[4]:= result[[1]] = %

and repeats the process to obtain:

Out[13]= {(v[2])'[t] == If[x[i] > 0, ex1, ex2], 
 
>    (v[3])'[t] == If[x[i] > 0, ex1, ex2], 
 
>    (v[4])'[t] == If[x[i] > 0, ex1, ex2], 
 
>    (v[5])'[t] == If[x[i] > 0, ex1, ex2], 
 
>    (v[6])'[t] == If[x[i] > 0, ex1, ex2]}

This result is a consequence, among other things, of If having
attribute HoldAll.  One way to change this behavior is to use
Evaluate:

In[14]:= Table[v[i]'[t] == If[ Evaluate[x[i]>0],ex1,ex2], {i, 2, 6}]

Out[14]= {(v[2])'[t] == If[x[2] > 0, ex1, ex2], 
 
>    (v[3])'[t] == If[x[3] > 0, ex1, ex2], 
 
>    (v[4])'[t] == If[x[4] > 0, ex1, ex2], 
 
>    (v[5])'[t] == If[x[5] > 0, ex1, ex2], 
 
>    (v[6])'[t] == If[x[6] > 0, ex1, ex2]}

(For additional information, refer to page 730 of the book.)

Dave Withoff
withoff at wri.com





  • Prev by Date: Re-indexing Mathematica
  • Next by Date: Re: Table[ ...If[...i...] ,{i,...}]
  • Previous by thread: Re: Table[ ...If[...i...] ,{i,...}]
  • Next by thread: Re: Table[ ...If[...i...] ,{i,...}]