|
|
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
|
|