Re: problem with append
- To: mathgroup at smc.vnet.net
- Subject: [mg129540] Re: problem with append
- From: <bar at robox.ii.up.krakow.pl>
- Date: Mon, 21 Jan 2013 00:10:22 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-newout@smc.vnet.net
- Delivered-to: mathgroup-newsend@smc.vnet.net
- References: <kdg2kv$hf8$1@smc.vnet.net>
Paolo cachecho <paolocach at gmail.com> 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]]]
> rr
>
> but rr remains empty. how can i solve this
> thank you
>
Try this:
r = {};
For[a = 2, a < 6, a++,
For[b = 2, b < 6, b++,
AppendTo[rr, a^b]]]
rr
or
Table[
Table[a^b, {b, 2, 5}], {a, 2, 5}]
Table[a^b, {a, 2, 5}, {b, 2, 5}]
but it is not the same, I'm not sure what You mean
remember : Table is faster then For , AppendTo is extremally slow
see
http://blog.wolfram.com/2011/12/07/10-tips-for-writing-fast-mathematica-code/
Try examples:
n = 200;
rr = {};
Timing@For[a = 2, a < n, a++,
For[b = 2, b < n, b++,
AppendTo[rr, a + b]]]
{18.4572, Null}
and
Timing[rr = Table[a + b, {a, 2, n - 1}, {b, 2, n - 1}];]
{0.104007, Null}
(I changed a^b -> a+b )
Olaf,