The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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, (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.

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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.