MathGroup Archive 2005

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

Search the Archive

Re: filling in an array


dumb_founded wrote:
> How can I input values into an array most efficiently?  I tried the
> code below, but it doesn't work.  The kernel starts working through the
> loop and never gets out.
> 
> Array[b, 10];
> For[i = 1, i = 10, b[i] = i/10]

Try using a functional style:

In[1]:= Function[{i},i/10] /@ Range[10]

         1   1  3   2  1  3  7   4  9
Out[1]= {--, -, --, -, -, -, --, -, --, 1}
         10  5  10  5  2  5  10  5  10

This maps the function "i -> i/10" over the Mathematica list "1..10".

The function can be written in Mathematica's elegant short-hand:

In[1]:= #/10& /@ Range[10]

Try to avoid programming styles used in imperative languages (such as C),
particularly constructs like "Do" and "For".

-- 
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com


  • Prev by Date: Re: filling in an array
  • Next by Date: Re: filling in an array
  • Previous by thread: Re: filling in an array
  • Next by thread: Re: filling in an array