MathGroup Archive 2013

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

Search the Archive

Re: problem with append

  • To: mathgroup at smc.vnet.net
  • Subject: [mg129531] Re: problem with append
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Mon, 21 Jan 2013 00:07:22 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net

On 1/20/13 at 1:21 AM, paolocach at gmail.com (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]]]
>rr

>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

rr={};
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]]

or

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