Re: Format for Do loop in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg41953] Re: Format for Do loop in Mathematica
- From: N.Ambert at ibmc.u-strasbg.fr (Nicolas Ambert)
- Date: Wed, 11 Jun 2003 13:17:39 -0400 (EDT)
- References: <bc465f$krd$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"Prasad" <haritha12 at attbi.com> wrote in message news:<bc465f$krd$1 at smc.vnet.net>... > Hi, > > I am using Mathematica and would like to know if it is possible to generate > a Do loop, similar to in Fortran. > > x=0 (*initializing x*) > y-0 (*initializing y*) > Do i = 1,10,1 (*i from 1 to 10 in steps of 1*) > x=x+5 (*increment x by 5*) > Do j=1,5,1 (*j from 1 to 5 in steps of 1*) > y=y+2 > Array1(i,j)=x+y > Array2(i,j)=x^2+y^2 > Print(x,y,Array1(i,j),Array2(i,j)) > Return > Return > > Could some one help to format the above steps in Mathematica or provide a > good reference example. Can the data in Print statement be saved in a Table > format, so that it could be exported as a text or data file. > > Thank You > Prasad Hi, Yes, it is possible to generate a Do loop, similar to in Fortran. This is one of the solutions : Clear[Array1, Array2, x, y, i, j] Array1 = Table[0, {i, 1, 10, 1}, {j, 1, 5, 1}]; (*initializing Array1 *) Array2 = Table[0, {i, 1, 10, 1}, {j, 1, 5, 1}]; (*initializing Array2 *) x = 0; y = 0; Do[ x = x + 5; Do[ y = y + 2; Array1[[i, j]] = x + y; Array2[[i, j]] = x^2 + y^2; Print[ x , " " , y, " ", Array1[[i, j]], " ", Array2[[i, j]]]; {x , y, Array1[[i, j]], Array2[[i, j]]} >>> "C:\\...\\FileName.txt"; (* write the data in a file *) , {j, 1, 5, 1}], {i, 1, 10, 1}] Nicolas Ambert.