Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Re: problem with append
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Bill Rowe

Posts: 1,647
Registered: 3/14/08
Re: problem with append
Posted: Jan 21, 2013 12:06 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 1/20/13 at 1:21 AM, paolocach@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.





Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.