Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #13242

Re: problem with append

From Alexei Boulbitch <Alexei.Boulbitch@iee.lu>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: problem with append
Date 2013-01-22 07:40 +0000
Message-ID <kdlfoh$10l$1@smc.vnet.net> (permalink)
Organization Time-Warner Telecom

Show all headers | View raw


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



Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: problem with append Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2013-01-22 07:40 +0000

csiph-web