|
|
Re: problem with append
Posted:
Jan 21, 2013 12:06 AM
|
|
On 1/20/13 at 1:21 AM, paolocach@gmail.com (Paolo cachecho) 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
There are two issues with the code above. First you set rr to a null list for every value of a,b. Second, Appendto should be AppendTo.
This will do what you want
rr={}; For[a = 2, a < 6, a++, For[b = 2, b < 6, b++, AppendTo[rr, a^b]]]
A faster more efficient way to get the same result would be
rr = Flatten@Outer[#1^#2&, Range[2,5],Range[2,5]]
or
rr = Flatten@Table[a^b,{a,2,5},{b,2,5}]
Using For generally results in slower running Mathematica code. AppendTo copies the entire array to add another element which will consume more memory and cpu resources.
|
|