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: problem with append
Replies: 9   Last Post: Jan 22, 2013 11:18 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Alexei Boulbitch

Posts: 480
Registered: 2/28/08
Re: problem with append
Posted: Jan 22, 2013 2:40 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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

Hi, Paolo,
You have problems with syntax. Instead try this:


Block[{r = {}},
For[a = 2, a < 6, a++, For[b = 2, b < 6, b++, AppendTo[r, a^b]]];
Print[r]
]

{4,8,16,32,9,27,81,243,16,64,256,1024,25,125,625,3125}

Or this:

a = {};
Block[{r = {}},
For[a = 2, a < 6, a++, For[b = 2, b < 6, b++, AppendTo[r, a^b]]];
a = r
]

{4, 8, 16, 32, 9, 27, 81, 243, 16, 64, 256, 1024, 25, 125, 625, 3125}

Take care that it is not "Appendto", but "AppendTo" that is important. However, in Mathematica the utilization of loops is usually discouraged. Use better Table, like this:

Table[i^j, {i, 2, 5}, {j, 2, 5}]


{{4, 8, 16, 32}, {9, 27, 81, 243}, {16, 64, 256, 1024}, {25, 125, 625,
3125}}

Table[i^j, {i, 2, 5}, {j, 2, 5}] // Flatten


{4, 8, 16, 32, 9, 27, 81, 243, 16, 64, 256, 1024, 25, 125, 625, 3125}

Have fun, Alexei


Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.boulbitch@iee.lu







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.