MathGroup Archive 2013

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

Search the Archive

Re: problem with append

  • To: mathgroup at
  • Subject: [mg129531] Re: problem with append
  • From: Bill Rowe <readnews at>
  • Date: Mon, 21 Jan 2013 00:07:22 -0500 (EST)
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:

On 1/20/13 at 1:21 AM, paolocach at (Paolo cachecho) wrote:

>hello i am new to mathmatica.i have the following problem if anyone
>could help.

>For[a = 2, a < 6, a++,
>For[b = 2, b < 6, b++, Appendto[rr = {}, a^b]]]

>but rr remains empty. how can i solve this thank you

There are two issues with the code above. First you set rr to a
null list for every value of a,b. Second, Appendto should be AppendTo.

This will do what you want

For[a = 2, a < 6, a++,
     For[b = 2, b < 6, b++,
   AppendTo[rr, a^b]]]

A faster more efficient way to get the same result would be

rr = Flatten@Outer[#1^#2&, Range[2,5],Range[2,5]]


rr = Flatten@Table[a^b,{a,2,5},{b,2,5}]

Using For generally results in slower running Mathematica code.
AppendTo copies the entire array to add another element which
will consume more memory and cpu resources.

  • Prev by Date: Re: problem with append
  • Next by Date: Re: Solid State Disk to boost Mathematica performance
  • Previous by thread: Re: problem with append
  • Next by thread: Re: problem with append