Search All of the Math Forum:

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

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

Topic: problem with append
Replies: 9   Last Post: Jan 22, 2013 11:18 PM

 Messages: [ Previous | Next ]
 Alexei Boulbitch Posts: 483 Registered: 2/28/08
Re: problem with append
Posted: Jan 22, 2013 2:40 AM

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

Date Subject Author
1/21/13 Chris Arthur
1/21/13 Bob Hanlon
1/22/13 David Bailey
1/21/13 Murray Eisenberg
1/21/13 KING NIGEL-NKI001
1/21/13 yehuda ben-shimol
1/21/13 Tomas Garza Hernandez