I'm generating a List that can be up to 26 values long, a simplified example is below. Is there a better way than nested loops?

 For[r = minr, r <= maxr, r = r + dr, 
   point[[1, 1]] = r;
   For[theta = 0, theta <= Pi, theta = theta + dthe, 
     point[[1, 2]] = theta;
     For[phi = -Pi, phi <= Pi, phi = phi + dphi, 
       point[[1, 3]] = phi; 
       val = myfunction[point]]]]

This really gets slow when I get ups to 5 nested loops.

  • 1
    $\begingroup$ It's great that you included your code, but could you also explain what you are trying to do? It's unclear why you're nesting loops at all, since the calculations inside the inside inner loops don't seem to reference the values in the outer loops. Furthermore, it seems like you keep assigning to point[[1, 1]] different values. So what exactly are you trying to do here? $\endgroup$
    – march
    Nov 13, 2015 at 20:58
  • $\begingroup$ As commented by march, please clarify your specific problem and add additional details to highlight exactly what you need as an end result. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question and then edit your question to make it better. $\endgroup$
    – rhermans
    Nov 13, 2015 at 21:05
  • $\begingroup$ It is the start of a grid search/error space mapping for a test function. I'm stepping through my solution space and will record the lowest and highest error at each radius step. $\endgroup$
    – Phymonkey
    Nov 13, 2015 at 21:27
  • $\begingroup$ So you are trying to maximize or minimize some function? $\endgroup$
    – bill s
    Nov 13, 2015 at 21:29
  • $\begingroup$ minimize, but the function is really nasty (takes about 3 hours with the minimization functions in Mathematica), I trying to get a quick estimate of the solution space to narrow down the domain. $\endgroup$
    – Phymonkey
    Nov 13, 2015 at 21:33

2 Answers 2


Table should do the trick.

Table[myfunction[{r, theta, phi}],
   {r, minr, maxr, dr},
   {theta, 0, π, dthe},
   {phi, -π, π, dphi}

This will produce a nested list of myfunction values. You may need to use Flatten on the result depending upon the shape that you want for the output.

  • $\begingroup$ Thanks I will give it a try as soon as can get logged in. $\endgroup$
    – Phymonkey
    Nov 14, 2015 at 18:59
  • $\begingroup$ Unfortunately, Mathematica wont let me us a list for the iterators part, and I would be stuck hard coding the Table. And how far out can you nest with Table? $\endgroup$
    – Phymonkey
    Nov 15, 2015 at 1:22
  • $\begingroup$ @Phymonkey - Mathematica supports using a list as an iterator. For example, Table[i, {i, {4, 7, 12}}] produces {4, 7, 12}. $\endgroup$ Nov 16, 2015 at 2:42
  • $\begingroup$ Ok, I was trying it a little different. $\endgroup$
    – Phymonkey
    Nov 16, 2015 at 16:31

Jack,I want thank you for your answer, it didn't give what I wanted but it was in the right direction. Here is the solution I have (one Do loop and a recursive function call) I could have done it with just one function but this was easier to debug, here are the functions:

Do[loop++; map[[loop]] = tablefunction[r, 2, inc, dim, range],
    {r, range[[1, 1]], range[[1, 2]], inc[[1]]}]

tablefunction[list_, index_, incr_, dim_, range_] := 
  Module[{map, nlist},
   If[index == 2,
    nlist = 
     Table[{list, ang}, {ang, range[[index, 1]], range[[index, 2]], 
    nlist = Partition[
      Flatten[Table[{list[[i]], ang}, {i, 1, Length[list]}, {ang, 
         range[[index, 1]], range[[index, 2]], 
         incr[[index]]}]], {index}]];
   If[index != dim, 
    map = exploremse[nlist, index + 1, incr, dim, range], 
    map = calculatemse[nlist]]; map];

It is about 4 times faster than the for loops but it will eat memory if dim gets over 5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.